CloudFormation リソースインポートを使用して既存の S3 バケットで Lambda 用の Amazon S3 通知設定を作成するにはどうすればよいですか?
既存の S3 バケットに AWS Lambda の Amazon Simple Storage Service (Amazon S3) 通知設定を作成したいと考えています。AWS CloudFormationを使用してリソースをインポートすることでこれを実行したいと考えています。
簡単な説明
カスタムリソースを使用せずに Amazon S3 通知を設定するには、次の操作を行います。
- Lambda 関数 S3NotificationLambdaFunction を使用してテンプレートを作成します。この関数は、既存のバケット NotificationS3Bucket 通知設定を追加します。
- リソースインポートを使用して、テンプレートで指定されている既存の NotificationS3Bucket S3 バケットを CloudFormation 管理に取り込みます。
- CloudFormation スタックを更新して、S3 バケットで有効にするプロパティを含めます。
カスタムリソースを使用するには、CloudFormation を使用して、既存の S3 バケットに Lambda の Amazon S3 通知設定を作成する方法を教えてくださいを参照してください。
解決方法
重要: 次の手順は、S3 バケット内の既存の通知設定または手動で作成された通知設定を上書きします。次の手順に従って、インポートされた S3 バケットに新しい通知設定を追加できます。
Lambda 関数を使用して CloudFormation テンプレートを作成する
次のサンプルテンプレートは、実行ロールと関数を呼び出す許可を持つ Lambda 関数を作成します。既存の Lambda 関数を使用するには、S3 バケットの LambdaConfiguration プロパティの CloudFormation テンプレートで関数の Amazon リソースネーム (ARN) を使用します。
AWSTemplateFormatVersion: 2010-09-09 Description: >- Sample template that shows you how to import an existing S3 bucket as an event source for a Lambda function Parameters: NotificationBucket: Type: String Description: S3 bucket that's used for Lambda event notification Resources: S3NotificationLambdaFunction: Type: 'AWS::Lambda::Function' Properties: Code: ZipFile: !Join - |+ - - import json - 'def lambda_handler(event,context):' - ' return ''Welcome... This is a test Lambda Function''' Handler: index.lambda_handler Role: !GetAtt LambdaIAMRole.Arn Runtime: python3.6 Timeout: 5 LambdaInvokePermission: Type: 'AWS::Lambda::Permission' Properties: FunctionName: !GetAtt S3NotificationLambdaFunction.Arn Action: 'lambda:InvokeFunction' Principal: s3.amazonaws.com SourceAccount: !Ref 'AWS::AccountId' SourceArn: !Sub 'arn:aws:s3:::${NotificationBucket}' LambdaIAMRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - 'sts:AssumeRole' Path: / Policies: - PolicyName: root PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - 's3:GetBucketNotification' - 's3:PutBucketNotification' Resource: !Sub 'arn:aws:s3:::${NotificationBucket}' - Effect: Allow Action: - 'logs:CreateLogGroup' - 'logs:CreateLogStream' - 'logs:PutLogEvents' Resource: 'arn:aws:logs:*:*:*'
既存の S3 バケットを CloudFormation スタックにインポートする
1. AWS CloudFormation コンソールを開きます。
2. ナビゲーションペインで [Stack] (スタック) を選択し、作成したスタックを選択します。
3. [Stack actions] (スタックアクション) を選択し、[Import resources into stack] (スタックへのリソースのインポート) を選択します。
4. [Import overview] (概要をインポート) ページを確認し、[Next] (次へ) を選択します。
重要: CloudFormation テンプレートでは、インポートする各リソースに DeletionPolicy 属性があり、他のすべてのリソースは同じままである必要があります。以下はその例です。
AWSTemplateFormatVersion: 2010-09-09 Description: >- Sample template that shows you how to import an existing S3 bucket as an event source for a Lambda function Parameters: NotificationBucket: Type: String Description: S3 bucket that's used for Lambda event notification Resources: S3NotificationLambdaFunction: Type: 'AWS::Lambda::Function' Properties: . . LambdaInvokePermission: Type: 'AWS::Lambda::Permission' Properties: . . LambdaIAMRole: Type: 'AWS::IAM::Role' Properties: . . <The preceding resources were already created. Now, add the DeletionPolicy to the preceding stack to import the S3 bucket.> NotificationS3Bucket: Type: 'AWS::S3::Bucket' DeletionPolicy: Retain Properties: BucketName: myenv-bucket #Bucket name to import
5. [Specify template] (テンプレートの指定) セクションで、[Amazon S3 URL] または要件に基づいて [Upload a template file] (テンプレートファイルのアップロード) を選択し、[Next] (次へ) を選択します。
6. ウィザードの残りの手順を実行して、既存のリソースをインポートします。詳細については、AWS CloudFormation コンソールを使用して既存のリソースをスタックにインポートするを参照してください。
7. スタックが IMPORT_COMPLETE 状態になるまで待ちます。
CloudFormation テンプレートを更新する
1. 変更した CloudFormation テンプレートを使用してスタックを更新します。
2. スタックが UPDATE_COMPLETE 状態になるまで待ち、S3 バケットの NotificationConfiguration を確認します。
3. S3 バケットの Amazon S3 イベント通知を有効にします。次のサンプルテンプレートでは、バケット名は myenv-bucket です。
AWSTemplateFormatVersion: 2010-09-09 Description: >- Sample template that shows you how to import an existing S3 bucket as an event source for a Lambda function Parameters: NotificationBucket: Type: String Description: S3 bucket that's used for Lambda event notification Resources: S3NotificationLambdaFunction: Type: 'AWS::Lambda::Function' Properties: . . LambdaInvokePermission: Type: 'AWS::Lambda::Permission' Properties: . . LambdaIAMRole: Type: 'AWS::IAM::Role' Properties: . . <The preceding resources were already created. Now, add the DeletionPolicy to the preceding stack to import the S3 bucket.> NotificationS3Bucket: Type: 'AWS::S3::Bucket' DeletionPolicy: Retain Properties: BucketName: myenv-bucket NotificationConfiguration: #Update stack with NotificationConfiguration LambdaConfigurations: - Event: 's3:ObjectCreated:Put' Function: !GetAtt S3NotificationLambdaFunction.Arn - Event: 's3:ObjectRemoved:*' Function: !GetAtt S3NotificationLambdaFunction.Arn
関連するコンテンツ
- 質問済み 5年前lg...
- 質問済み 4年前lg...
- 質問済み 14日前lg...
- 質問済み 6ヶ月前lg...
- AWS公式更新しました 1年前