Import existing resource into new cloudformation stack

0

I have an existing S3 bucket that I would like to import into a new cloudformation stack. ExistingDestinationBucket already exists.

The AWS docs I have found detail creating a new stack entirely from existing resources, or adding existing resources to an existing stack, neither of which do what I need. I need to include the S3 bucket in the stack so I can define a trigger for the lambda function. My cloudformation template is below.

When running as an import, AWS asks me to identify the existing lambda function - which doesn't exist. When running as all new resources, I get an error that the bucket already exists.

Is there any way to do what I'm trying to do?

AWSTemplateFormatVersion: "2010-09-09"
Description: "Lighthouse run infrastructure"
Resources:

    ExistingDestinationBucket:
        Type: "AWS::S3::Bucket"
        DeletionPolicy: "Retain"
        Properties:
            BucketName: !Sub "rapidspike-pages-lambda-${AWS::Region}"
            NotificationConfiguration:
                LambdaConfigurations:
                    Event: "s3:ObjectCreated:*"
                    Filter: "*/summary.json"
                    Function: !Ref PostProcessingFunc

    PostProcessingFunc:
        Type: "AWS::Lambda::Function"
        Properties:
            Description: ""
            FunctionName: "lighthouse-run-post-processing"
            Handler: "src/handlers/handler.handler"
            Code:
                S3Bucket: !Sub "lighthouse-run-functions-ap-south-1"
                S3Key: "postprocess.zip"
            MemorySize: 512
            Runtime: "nodejs16.x"
            Timeout: 60
            TracingConfig:
                Mode: "PassThrough"
            EphemeralStorage:
                Size: 512
1개 답변
1
수락된 답변

Hi,

Yes there is a way to do what you want to do but it would be done in multiple steps.

Step 1: Update your template you have shown above to be JUST the S3 Bucket and no other resources in the stack. Create a new Stack using this template using the Import option.

The result of step 1 will be a Stack that has a single resource which is an imported resource of the existing S3 bucket

Step 2: Replace the resources back into the template that you removed in Step 1 and then perform a Stack Update on your existing stack (not an import)

This will add the new resources (the Lambda function) to the stack and after the update is complete you should have the situation you wanted from the start.

I hope this helps and makes sense.

profile pictureAWS
danjhd
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠