如何使用 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 函數,請在 CloudFormation 範本中,針對 S3 儲存貯體中的 LambdaConfiguration 屬性使用函數的 Amazon Resource Name (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. 在導覽窗格中,選擇「堆疊」,然後選取您建立的堆疊。
3. 選擇「堆疊動作」,然後選擇「將資源匯入堆疊」。
4. 檢閱「匯入概觀頁面」,然後選擇「下一步」。
**重要事項:**在 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. 在「指定範本」區段中,選擇 Amazon S3 URL 或根據您的需求上傳範本檔案,然後選擇「下一步」。
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
相關內容
- 已提問 9 個月前lg...
- 已提問 9 個月前lg...
- 已提問 1 年前lg...
- 已提問 3 個月前lg...
- 已提問 1 年前lg...
- AWS 官方已更新 10 個月前
- AWS 官方已更新 1 年前
- AWS 官方已更新 6 個月前
- AWS 官方已更新 1 年前