1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Udemy】AWS-SAA-C03 ソリューションアーキテクトアソシエイト

Last updated at Posted at 2022-04-14

Udemyで公開しているAWSS-SAA-C03対策動画について、動画内のソースやリンクはこちらでまとめます。

動画

ソース

動画内で使ったコードです。

CloudWatch Logs、Amazon S3、および Amazon SNS にアクセスするためのアクセス許可を関数に付与するIAMロール

IAMロール
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
		"logs:*"
		],
            "Resource": [
                "arn:aws:s3:logs:*:*:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
             ],
            "Resource": [
                "arn:aws:s3:::my-bucket/*"
        },
           "Action": [
		"sns:Publish"
		],
            "Resource": [
                "arn:aws:sns:us-west-2:123456789012:my-tocip"
        }
    ]
}

CloudTrailログを処理し、新しいAmazon SNSトピックが作成されるたびに Amazon SNS から通知するLambda関数

Lambda関数
var aws  = require('aws-sdk');
var zlib = require('zlib');
var async = require('async');

var EVENT_SOURCE_TO_TRACK = /sns.amazonaws.com/;
var EVENT_NAME_TO_TRACK   = /CreateTopic/;
var DEFAULT_SNS_REGION  = 'us-east-2';
var SNS_TOPIC_ARN       = 'arn:aws:sns:us-west-2:123456789012:my-topic';

var s3 = new aws.S3();
var sns = new aws.SNS({
    apiVersion: '2010-03-31',
    region: DEFAULT_SNS_REGION
});

exports.handler = function(event, context, callback) {
    var srcBucket = event.Records[0].s3.bucket.name;
    var srcKey = event.Records[0].s3.object.key;

    async.waterfall([
        function fetchLogFromS3(next){
            console.log('Fetching compressed log from S3...');
            s3.getObject({
               Bucket: srcBucket,
               Key: srcKey
            },
            next);
        },
        function uncompressLog(response, next){
            console.log("Uncompressing log...");
            zlib.gunzip(response.Body, next);
        },
        function publishNotifications(jsonBuffer, next) {
            console.log('Filtering log...');
            var json = jsonBuffer.toString();
            console.log('CloudTrail JSON from S3:', json);
            var records;
            try {
                records = JSON.parse(json);
            } catch (err) {
                next('Unable to parse CloudTrail JSON: ' + err);
                return;
            }
            var matchingRecords = records
                .Records
                .filter(function(record) {
                    return record.eventSource.match(EVENT_SOURCE_TO_TRACK)
                        && record.eventName.match(EVENT_NAME_TO_TRACK);
                });

            console.log('Publishing ' + matchingRecords.length + ' notification(s) in parallel...');
            async.each(
                matchingRecords,
                function(record, publishComplete) {
                    console.log('Publishing notification: ', record);
                    sns.publish({
                        Message:
                            'Alert... SNS topic created: \n TopicARN=' + record.responseElements.topicArn + '\n\n' +
                            JSON.stringify(record),
                        TopicArn: SNS_TOPIC_ARN
                    }, publishComplete);
                },
                next
            );
        }
    ], function (err) {
        if (err) {
            console.error('Failed to publish notifications: ', err);
        } else {
            console.log('Successfully published all notifications.');
        }
        callback(null,"message");
    });
};

参考: Lambdaのハンズオン
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-cloudtrail-example.html

リンク

関連リンク

テキスト

この1冊で合格! AWS認定ソリューションアーキテクト
https://www.amazon.co.jp/dp/4046042036/
徹底攻略 AWS認定 ソリューションアーキテクト − アソシエイト教科書
https://www.amazon.co.jp/dp/4295010650/
AWS認定資格試験テキスト AWS認定ソリューションアーキテクト
https://www.amazon.co.jp/dp/4815607389/

お役立ちサイト

AWS公式
https://aws.amazon.com/jp/
ざっくりAWS
https://aws-rough.cc/
SlideShare: AWS
https://www.slideshare.net/AmazonWebServicesJapan
GitLab on AWS
https://about.gitlab.com/partners/technology-partners/aws/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?