Skip to content

AWS CodeBuild examples (AWS SDK for JS v3) #1940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions javascriptv3/example_code/codebuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# AWS CodeBuild JavaScript SDK v3 code examples
AWS CodeBuild is a fully-managed source control service that makes it easy for companies to host secure and highly scalable private Git repositories.
## Code examples
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) AWS CodeBuild examples:

- [Create a project](src/createProject.js)

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

## Getting started

1. Clone the [AWS SDK Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment. See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for instructions.

2. Install the dependencies listed in the package.json.

```
npm install node -g
cd javascriptv3/example_code/CodeBuild
npm install
```
3. In your text editor, update user variables specified in the ```Inputs``` section of the sample file.

4. Run sample code:
```
cd src
node [example name].js
```

## Unit tests
For more information see, the [README](../README.rst).

## Resources
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3)
- [AWS SDK for JavaScript v3 API Reference Guide](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-CodeBuild/index.html)
- [AWS CodeBuild user guide](https://docs.aws.amazon.com/CodeBuild/latest/userguide/welcome.html)
70 changes: 70 additions & 0 deletions javascriptv3/example_code/codebuild/src/createProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
which is codeBuild.JavaScript.createProjectV3available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeBuild works, see https://docs.aws.amazon.com/codebuild/latest/userguide/concepts.html.

Purpose:
createProject.js demonstrates how to create an AWS CodeBuild project.

Inputs (replace in code):
- TYPE_OF_BUILD_OUTPUT
- COMPUTE_TYPE
- IMAGE_NAME
- ENVIRONMENT_TYPE
- PROJECT_NAME
- IAM_ROLE
- SOURCE_TYPE
- LOCATION_TO_BUILD_SOURCE_CODE

Note: This sample specifies the mimimum parameters required, and depending on your choices, alternative parameters may be required.
For further details on all available parameters, see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CodeBuild.html#createProject-property. (This is for V2, but V3 has similar
parameters).

Running the code:
node createProject.js
*/
// snippet-start:[codeBuild.JavaScript.createProjectV3]
// Get service clients module and commands using ES6 syntax.
import { CreateProjectCommand } from "@aws-sdk/client-codebuild";
import { codeBuildClient } from "./libs/codeBuildClient.js";

// Set the bucket parameters.

export const params = {
artifacts: { /* required */
/* Required. Options include CODEPIPELINE | S3 | NO_ARTIFACTS. */
type: "CODEPIPELINE"
},
environment: { /* required */
/* Required. Options include BUILD_GENERAL1_SMALL | BUILD_GENERAL1_MEDIUM | BUILD_GENERAL1_LARGE | BUILD_GENERAL1_2XLARGE. */
computeType: "BUILD_GENERAL1_SMALL",
image: 'myiage', /* Required. */
/* Required. Options include WINDOWS_CONTAINER | LINUX_CONTAINER | LINUX_GPU_CONTAINER | ARM_CONTAINER | WINDOWS_SERVER_2019_CONTAINER. */
type: "BUILD_GENERAL1_SMALL"
},
name: 'mytestproject', /* Required. */
/* Required. This AWS Identity and Access Management (IAM) role must have permissions to create an AWS CodeBuild project. */
serviceRole: 'IAM_ROLE',
source: { /* required */
type: "GITHUB", /* Required. Options include CODECOMMIT | CODEPIPELINE | GITHUB | S3 | BITBUCKET | GITHUB_ENTERPRISE | NO_SOURCE */
auth: {
type: "OAUTH" /* Required. 'OAUTH' is the only valid value. */
},
location: "LOCATION_TO_BUILD_SOURCE_CODE" /* Required. */
}
};

// Create the AWS CodeBuild project.
export const run = async () => {
try {
const data = await codeBuildClient.send(new CreateProjectCommand(params));
console.log("Success", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
run();
// snippet-end:[codeBuild.JavaScript.createProjectV3]
// For unit tests only.
// module.exports ={run, bucketParams};
22 changes: 22 additions & 0 deletions javascriptv3/example_code/codebuild/src/libs/codeBuildClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
which is available at https://github.com/aws/aws-sdk-js-v3.

Purpose:
codeBuildClient.js is a helper function that creates the Amazon CodeBuild service clients.

Inputs (replace in code):
- REGION

*/
// snippet-start:[code-build.JavaScript.codeBuildClient]
import { CodeBuildClient } from "@aws-sdk/client-codebuild";

const REGION = "REGION";

// Create an AWS CodeBuild service client object.
const codeBuildClient = new CodeBuildClient({region: REGION});

export { codeBuildClient };
// snippet-end:[code-build.JavaScript.codeBuildClient]
11 changes: 11 additions & 0 deletions javascriptv3/example_code/codebuild/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "aws-sdk-v3-codebuild-examples",
"version": "1.0.0",
"repository": "[email protected]/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/codebuild.git",
"author": "Brian Murray <[email protected]>",
"license": "Apache 2.0",
"dependencies": {
"@aws-sdk/client-codebuild": "^3.3.0"
},
"type": "module"
}
3 changes: 3 additions & 0 deletions javascriptv3/example_code/codebuild/tests/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env']
}
15 changes: 15 additions & 0 deletions javascriptv3/example_code/codebuild/tests/createProject.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
jest.mock("../src/libs/codeBuildClient.js");
jest.mock("@aws-sdk/client-codebuild");

// Get service clients module and commands.
import 'regenerator-runtime/runtime'
import { run, params } from "../src/createProject";
import { codeBuildClient } from "../src/libs/codeBuildClient";

describe("@aws-sdk/client-codebuild mock", () => {
it("should successfully mock CodeBuild client", async () => {
codeBuildClient.send.mockResolvedValue({ isMock: true });
const response = await run(params);
expect(response.isMock).toEqual(true);
});
});
8 changes: 8 additions & 0 deletions javascriptv3/example_code/codebuild/tests/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
moduleDirectories: ["node_modules"],
testMatch: ["**/tests/*.test.js"],
transform: {
"^.+\\.(js|jsx)$": "babel-jest"
}
};
19 changes: 19 additions & 0 deletions javascriptv3/example_code/codebuild/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "aws-sdk-v3-cloudwatch-examples",
"version": "1.0.0",
"main": "index.js",
"repository": "[email protected]/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/cloudwatch.git",
"author": "Brian Murray <[email protected]>, Alex Forsyth <[email protected]>",
"license": "Apache 2.0",
"dependencies": {
"@aws-sdk/client-codebuild": "^3.3.0"
},
"devDependencies": {
"@babel/preset-env": "^7.14.4",
"jest": "^26.6.3",
"regenerator-runtime": "^0.13.7"
},
"scripts": {
"test": "jest"
}
}
1 change: 0 additions & 1 deletion javascriptv3/example_code/codecommit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ For more information see, the [README](../README.rst).

## Resources
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3)
- [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-examples.html)
- [AWS SDK for JavaScript v3 API Reference Guide](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-codecommit/index.html)
- [AWS CodeCommit user guide](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)
3 changes: 0 additions & 3 deletions javascriptv3/example_code/codecommit/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
"dependencies": {
"@aws-sdk/client-codecommit ": "^3.3.0"
},
"scripts": {
"test": "jest"
},
"type": "module"
}
26 changes: 26 additions & 0 deletions ruby/example_code/sns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Amazon Cloudwatch JavaScript SDK v3 code examples
Amazon Cloudwatch enables you to collect, access, and correlate this data on a single platform from across all your AWS resources, applications, and services.

## Code examples
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) Amazon Cloudwatch examples:

- [Create a subscription](./sns-ruby-example-create-subscription.rb)
- [Create a topic](./sns-ruby-example-create-topic.rb)
- [Enable a resource](./sns-ruby-example-enable-resource.rb)
- [Send a message](./sns-ruby-example-send-message.rb)
- [List subscriptions](./sns-ruby-example-show-subscriptions.rb)
- [List topics](./sns-ruby-example-show-topics.rb)


## Getting started

1. Clone the [AWS Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment.
See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for
instructions.


## Resources
- [AWS SDK for Ruby repo](https://github.com/aws/aws-sdk-ruby) .
- [AWS SDK for Ruby Developer Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/welcome.html)
- [AWS SDK for Ruby v3 API Reference Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/)

71 changes: 41 additions & 30 deletions ruby/example_code/sns/sns-ruby-example-create-subscription.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Subscribes a user to an SNS topic.]
# snippet-keyword:[Amazon Simple Notification Service]
# snippet-keyword:[topic method]
# snippet-keyword:[Topic.subscribe method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[sns]
# snippet-keyword:[Code Sample]
# snippet-sourcetype:[full-example]
# snippet-sourcedate:[2018-03-16]
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Purpose:
# sns-ruby-example-create-subscription.rb demonstrates how to create an Amazon Simple Notification Services (SNS) subscription using
# the AWS SDK for JavaScript (v3).

# Inputs:
# - REGION
# - SNS_TOPIC_ARN
# - EMAIL_ADDRESS

# snippet-start:[sns.Ruby.createSubscription]

require 'aws-sdk-sns' # v2: require 'aws-sdk'

sns = Aws::SNS::Resource.new(region: 'us-west-2')
def subscription_created?(sns_client, topic_arn, protocol, endpoint)

sns_client.subscribe(topic_arn: topic_arn, protocol: protocol, endpoint: endpoint)

rescue StandardError => e
puts "Error while creating the subscription: #{e.message}"
end

# Full example call:
def run_me

protocol = 'email'
endpoint = 'EMAIL_ADDRESS'
topic_arn = 'TOPIC_ARN'
region = 'REGION'

sns_client = Aws::SNS::Client.new(region: region)

puts "Creating the subscription."

topic = sns.topic('arn:aws:sns:us-west-2:123456789:MyGroovyTopic')
if subscription_created?(sns_client, topic_arn, protocol, endpoint)
puts 'The subscriptions was created.'
else
puts 'The subscription was not created. Stopping program.'
exit 1
end
end

sub = topic.subscribe({
protocol: 'email',
endpoint: '[email protected]'
})
run_me if $PROGRAM_NAME == __FILE__

puts sub.arn
# snippet-end:[sns.Ruby.createSubscription]
62 changes: 37 additions & 25 deletions ruby/example_code/sns/sns-ruby-example-create-topic.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Creates an SNS topic.]
# snippet-keyword:[Amazon Simple Notification Service]
# snippet-keyword:[create_topic method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[sns]
# snippet-keyword:[Code Sample]
# snippet-sourcetype:[full-example]
# snippet-sourcedate:[2018-03-16]
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Purpose:
# sns-ruby-example-create-topic.rb demonstrates how to create an Amazon Simple Notification Services (SNS) topic using
# the AWS SDK for JavaScript (v3).

# Inputs:
# - REGION
# - TOPIC_NAME

# snippet-start:[sns.Ruby.createTopic]

require 'aws-sdk-sns' # v2: require 'aws-sdk'

sns = Aws::SNS::Resource.new(region: 'us-west-2')
def topic_created?(sns_client, topic_name)

sns_client.create_topic(name: topic_name)
rescue StandardError => e
puts "Error while creating the topic named '#{topic_name}': #{e.message}"
end

# Full example call:
def run_me
topic_name = 'TOPIC_NAME'
region = 'REGION'

sns_client = Aws::SNS::Client.new(region: region)

puts "Creating the topic '#{topic_name}'..."

if topic_created?(sns_client, topic_name)
puts 'The topic was created.'
else
puts 'The topic was not created. Stopping program.'
exit 1
end
end

topic = sns.create_topic(name: 'MyGroovyTopic')
puts topic.arn
run_me if $PROGRAM_NAME == __FILE__
# snippet-end:[sns.Ruby.createTopic]
Loading