Come posso utilizzare un'importazione di risorse CloudFormation per creare una configurazione di notifica Amazon S3 per Lambda su un bucket S3 esistente?

4 minuti di lettura
0

Desidero creare una configurazione di notifica Amazon Simple Storage Service (Amazon S3) per AWS Lambda su un bucket S3 esistente. Voglio farlo utilizzando AWS CloudFormation per importare una risorsa.

Breve descrizione

Per configurare una notifica Amazon S3 senza utilizzare una risorsa personalizzata, procedi come segue:

  1. Crea un modello con la funzione Lambda S3NotificationLambdaFunction. Questa funzione aggiunge la configurazione di notifica bucket NotificationS3Bucket esistente.
  2. Utilizza un'importazione di risorse per inserire il bucket S3 NotificationS3bucket esistente specificato nel modello nella gestione di CloudFormation.
  3. Aggiorna lo stack CloudFormation per includere le proprietà che desideri attivare nel tuo bucket S3.

Per utilizzare una risorsa personalizzata, consulta Come posso usare CloudFormation per creare una configurazione di notifica Amazon S3 per Lambda su un bucket S3 esistente?

Risoluzione

Importante: i passaggi seguenti sovrascrivono qualsiasi configurazione di notifica esistente o creata manualmente in un bucket S3. Segui questi passaggi per aggiungere una nuova configurazione di notifica a un bucket S3 importato.

Crea un modello CloudFormation con una funzione Lambda

Il modello di esempio seguente crea una funzione Lambda con un ruolo in esecuzione e le autorizzazioni per richiamare la funzione. Per utilizzare una funzione Lambda esistente, utilizza il nome della risorsa Amazon (ARN) della funzione nel modello CloudFormation per la proprietà LambdaConfiguration nel bucket S3.

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:*:*:*'

Importa un bucket S3 esistente nel tuo stack CloudFormation

1.    Apri la console di AWS CloudFormation.

  1. Nel riquadro di navigazione, scegli Stack, quindi seleziona lo stack che hai creato.

  2. Scegli Stack actions, quindi scegli Importa risorse nello stack.

  3. Controlla la pagina Panoramica delle importazioni, quindi scegli Avanti.

Importante: nel tuo modello CloudFormation, ogni risorsa che importi deve avere un attributo DeletionPolicy e tutte le altre risorse devono rimanere invariate. Ad esempio:

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
  1. Nella sezione Specifica il modello, scegli l'URL di Amazon S3 o Carica un file modello in base ai tuoi requisiti, quindi scegli Avanti.

  2. Completa il resto dei passaggi della procedura guidata per importare le risorse esistenti. Per ulteriori informazioni, consulta Importare una risorsa esistente in uno stack utilizzando la console AWS CloudFormation.

  3. Attendi che lo stack raggiunga lo stato IMPORT_COMPLETE.

Aggiorna il tuo modello CloudFormation

  1. Aggiorna lo stack con il modello CloudFormation che hai modificato.

  2. Attendi che lo stack raggiunga lo stato UPDATE_COMPLETE, quindi verifica la NotificationConfiguration sul bucket S3.

  3. Attiva le notifiche degli eventi Amazon S3 sul tuo bucket S3. Nel seguente modello di esempio, il nome del bucket è 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 UFFICIALE
AWS UFFICIALEAggiornata un anno fa