Skip to content
23 changes: 23 additions & 0 deletions javascript/example_code/cloudwatch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Amazon Cloudwatch JavaScript SDK v2 code examples
Amazon Cloudwatch enables you to collect, access, and correlate 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 2 (v2) Amazon Cloudwatch examples:

- [Delete CloudWatch alarms](./cw_deletealarms.js)
- [Describe CloudWatch alarms](./cw_describealarms.js)
- [Disable CloudWatch alarm actions](./cw_disablealarmactions.js)
- [Enable CloudWatch alarm actions](./cw_enablealarmactions.js)
- [List CloudWatch metrics](./cw_listmetrics.js)
- [Put CloudWatch metric alarms](./cw_putmetricalarm.js)
- [Put CloudWatch metric data](./cw_putmetricdata.js)
- [Put CloudWatch events](./cwe_putevents.js)
- [Put CloudWatch event rule](./cwe_putRule.js)
- [Put CloudWatch event targets](./cwe_putTargets.js)
- [Delete a CloudWatch log subscription filters](./cwe_deletesubscriptionfilter.js)
- [Describe CloudWatch log subscription filters](./cwe_describesubscriptionfilters.js)
- [Put CloudWatch log subscription filter](./cwe_putsubscriptionfilter.js)

## Resources

- [AWS SDK for JavaScript v2 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/cloudwatch-examples.html)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
This is a workspace where you can find an AWS SDK for JavaScript version 3 (v3) tutorial that demonstrates how to
use Amazon CloudWatch scheduled events to invoke AWS Lambda functions.

The [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/invoke-lambda-functions-with-scheduled-events.html) contains these examples.
The [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/scheduled-events-invoking-lambda-example.html)
contains instructions for running this example.

# Getting started

Expand All @@ -19,7 +20,7 @@ which are *@aws-sdk/client-cognito-identity*, *@aws-sdk/credential-provider-cog
They also include third-party Node.js modules, including webpack.
```
npm install ts-node -g # If using JavaScript, enter 'npm install node -g' instead
cd javascriptv3/example_code/cross-services/lamba-api-gateway
cd javascriptv3/example_code/cross-services/lambda-scheduled-events
npm install
```

Expand Down
2 changes: 1 addition & 1 deletion ruby/example_code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This folder contains code examples that demonstrate how to use the AWS SDK for R
[Install the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-install.html) in the
*AWS SDK for Ruby Developer Guide*.

## Cautions
## Important

- As an AWS best practice, grant this code least privilege, or only the
permissions required to perform a task. For more information, see
Expand Down
76 changes: 76 additions & 0 deletions ruby/example_code/cloudtrail/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# AWS SDK for Ruby code examples for AWS CloudTrail

## Purpose

This folder contains code examples that demonstrate how to use the AWS SDK for Ruby to automate AWS CloudTrail.

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

### API examples
- [Create a trail](./aws-ruby-sdk-cloudtrail-example-create-trail.rb)
- [Delete a trail](./aws-ruby-sdk-cloudtrail-example-delete-trail.rb)
- [Describe trails](./aws-ruby-sdk-cloudtrail-example-describe-trails.rb)
- [Lookup events](./aws-ruby-sdk-cloudtrail-example-lookup-events.rb)

## Important

- As an AWS best practice, grant this code least privilege, or only the
permissions required to perform a task. For more information, see
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
in the *AWS Identity and Access Management User Guide*.
- This code has not been tested in all AWS Regions. Some AWS services are
available only in specific AWS Regions. For more information, see the
[AWS Regional Services List](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)
on the AWS website.
- Running this code might result in charges to your AWS account.

## Running the code

Most of these code example files can be run with very little to no modification. For example, to use Ruby to run the `cw-ruby-example-create-alarm.rb` file, replace the hard-coded values in the file with your own values, save the file, and then run the file. For example:

```
ruby cw-ruby-example-create-alarm.rb
```

Most of these files have been refactored into reusable functions that can be copied into your own code. You can then call those functions directly from your own code without modifying the copied function code itself. For example, you could copy the `alarm_created_or_updated?` function code from the `cw-ruby-example-create-alarm.rb` file into your own code. You could then adapt the code in the `run_me` function in that same file as a basis to write your own code to call the copied `alarm_created_or_updated?` function.

## Running the tests

### Prerequisites

- An AWS account. To create an account, see [How do I create and activate a new AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) on the AWS Premium Support website.
- AWS credentials or an AWS Security Token Service (AWS STS) access token. For details, see
[Configuring the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html) in the
*AWS SDK for Ruby Developer Guide*.
- To run the code examples, Ruby version 1.9 or later. For Ruby download and installation instructions, see
[Download Ruby](https://www.ruby-lang.org/en/downloads/) on the Ruby Progamming Language website.
- To test the code examples, RSpec 3.9 or later. For RSpec download and installation instructions, see the [rspec/rspec](https://github.com/rspec/rspec) repository in GitHub.
- The AWS SDK for Ruby. For AWS SDK for Ruby download and installation instructions, see
[Install the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-install.html) in the
*AWS SDK for Ruby Developer Guide*.


Most of these code example files have accompanying tests that are written to work with RSpec. These tests are in the `tests` folder and contain the same file name as the corresponding code example file, for example `tests/test_cw-ruby-example-create-alarm.rb` contains tests for `cw-ruby-example-create-alarm.rb`.

To use RSpec to run all tests within a file, specify the path to that file, for example:

```
rspec tests/test_cw-ruby-example-create-alarm.rb
```

To explore additional options for using RSpec to run tests, run the `rspec --help` command.

Most of these tests are designed to use stubs, to avoid generating unnecessary costs in an AWS account. For more information, see [Stubbing Client Responses and Errors](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/stubbing.html) in the *AWS SDK for Ruby Developer Guide*.


## Additional information
- [AWS CloudTrail Documentation](https://docs.aws.amazon.com/cloudtrail/)
- [AWS SDK for Ruby Documentation](https://docs.aws.amazon.com/sdk-for-ruby)
- [AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs)
- [RSpec Documentation](https://rspec.info/documentation)

---
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Creates a CloudTrail trail.]
# snippet-keyword:[AWS CloudTrail]
# snippet-keyword:[create_trail method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[cloudtrail]
# 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:
# aws-ruby-sdk-cloudtrail-example-create-trail.rb demonstrates how to create
# an Amazon CloudTrail trail using the AWS SDK for Ruby.

# Inputs:
# - REGION

# snippet-start:[cloudtrail.Ruby.createTrail]
require 'aws-sdk-cloudtrail' # v2: require 'aws-sdk'
require 'aws-sdk-s3'
require 'aws-sdk-sts'

# Attach IAM policy to bucket
def add_policy(bucket)
# Get account ID using STS
sts_client = Aws::STS::Client.new(region: 'us-west-2')
sts_client = Aws::STS::Client.new(region: 'REGION')
resp = sts_client.get_caller_identity({})
account_id = resp.account

# Attach policy to S3 bucket
# Attach policy to and Amazon Simple Storage Solution (S3) bucket.
# Replace us-west-2 with the AWS Region you're using for AWS CloudTrail.
s3_client = Aws::S3::Client.new(region: 'us-west-2')

begin
Expand Down Expand Up @@ -126,3 +115,4 @@ def add_policy(bucket)
puts err
exit 1
end
# snippet-end:[cloudtrail.Ruby.createTrail]
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Deletes a CloudTrail trail.]
# snippet-keyword:[AWS CloudTrail]
# snippet-keyword:[delete_trail method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[cloudtrail]
# 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:
# aws-ruby-sdk-cloudtrail-example-delete-trail.rb demonstrates how to delete
# an Amazon CloudTrail trail using the AWS SDK for Ruby.

# snippet-start:[cloudtrail.Ruby.deleteTrail]

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

Expand All @@ -31,6 +17,7 @@
name = ARGV[0]

# Create client in us-west-2
# Replace us-west-2 with the AWS Region you're using for AWS CloudTrail.
client = Aws::CloudTrail::Client.new(region: 'us-west-2')

begin
Expand All @@ -44,3 +31,4 @@
puts err
exit 1
end
# snippet-end:[cloudtrail.Ruby.deleteTrail]
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Lists your CloudTrail trails.]
# snippet-keyword:[AWS CloudTrail]
# snippet-keyword:[describe_trails method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[cloudtrail]
# 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:
# aws-ruby-sdk-cloudtrail-example-describe-trails.rb demonstrates how to
# retrieve details about your an Amazon CloudTrail trails using the AWS SDK for Ruby.


# snippet-start:[cloudtrail.Ruby.describeTrails]
require 'aws-sdk-cloudtrail' # v2: require 'aws-sdk'

# Create client in us-west-2
client = Aws::CloudTrail::Client.new(region: 'us-west-2')
# Create client in us-west-2.
# Replace us-west-2 with the AWS Region you're using for AWS CloudTrail.

client = Aws::CloudTrail::Client.new(region: 'REGION')

resp = client.describe_trails({})

puts
puts "Found #{resp.trail_list.count} trail(s) in us-west-2:"
puts "Found #{resp.trail_list.count} trail(s) in REGION:"
puts

resp.trail_list.each do |trail|
puts 'Name: ' + trail.name
puts 'S3 bucket name: ' + trail.s3_bucket_name
puts
end
# snippet-end:[cloudtrail.Ruby.describeTrails]
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourceauthor:[Doug-AWS]
# snippet-sourcedescription:[Lists your CloudTrail events.]
# snippet-keyword:[AWS CloudTrail]
# snippet-keyword:[lookup_events method]
# snippet-keyword:[Ruby]
# snippet-sourcesyntax:[ruby]
# snippet-service:[cloudtrail]
# 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:
# aws-ruby-sdk-cloudtrail-example-lookup-events.rb demonstrates how to look up
# Amazon CloudTrail trail events using the AWS SDK for Ruby.


# snippet-start:[cloudtrail.Ruby.lookupEvents]

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

Expand All @@ -38,7 +25,8 @@ def show_event(event)
end
end

# Create client in us-west-2
# Create client in us-west-2.
# Replace us-west-2 with the AWS Region you're using for AWS CloudTrail.
client = Aws::CloudTrail::Client.new(region: 'us-west-2')

resp = client.lookup_events()
Expand All @@ -50,3 +38,4 @@ def show_event(event)
resp.events.each do |e|
show_event(e)
end
# snippet-end:[cloudtrail.Ruby.lookupEvents]
30 changes: 0 additions & 30 deletions ruby/example_code/cloudtrail/metadata.yaml

This file was deleted.

Loading