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 Answer
1
Accepted Answer

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
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions