CloudFormation 리소스 가져오기를 사용하여 기존 S3 버킷에서 Lambda를 위한 Amazon S3 알림 구성을 생성하려면 어떻게 해야 합니까?

4분 분량
0

AWS Lambda 함수에 대한 Amazon Simple Storage Service(Amazon S3) 알림 구성을 기존 S3 버킷에 생성하려고 합니다. 이 작업을 수행하기 위해 AWS CloudFormation을 사용하여 리소스를 가져오고 싶습니다.

간략한 설명

사용자 지정 리소스를 사용하지 않고 Amazon S3 알림을 구성하려면 다음을 수행합니다.

  1. Lambda 함수 S3NotificationLambdaFunction을 사용하여 템플릿을 생성합니다. 이 함수는 기존 버킷 NotificationS3Bucket 알림 구성을 추가합니다.
  2. 리소스 가져오기를 사용하여, 템플릿에 지정된 기존 NotificationS3Bucket S3 버킷을 AWS CloudFormation 관리로 가져옵니다.
  3. S3 버킷에서 활성화하려는 속성을 포함하도록 CloudFormation 스택을 업데이트합니다.

사용자 지정 리소스를 사용하려면 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 page(가져오기 개요 페이지)를 검토한 후 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

AWS 공식
AWS 공식업데이트됨 일 년 전