Home > database >  Is there a way to modularize cloudformation template?
Is there a way to modularize cloudformation template?

Time:05-09

Right now I am using a template in such a way:

  1. Create initial resources
  2. Import existing resources (S3)
  3. Update stack with new resources depending on existing resources

This is boring because I have to deploy the stack in a 3-step process, commenting out depending on resources.

I've read about cloudformation modules but they don't seem to solve the problem with merging 3-step process (init, import existing, use existing) into single cloudformation template.

Is there a solution for such a case?

UPD: I do understand that I can just use resource ARN to reference existing resources without importing them but it doesn't solve the case where I need to attach additional things to my resources (here I create event notification to imported S3 bucket):

# Import existing resource
S3DataBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
# Modify template by adding event configuration
S3DataBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    Properties:
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: s3:ObjectCreated:Put
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: !Ref SomePrefix
            Function: !GetAtt SomeLambda.Arn

CodePudding user response:

Looks like there is an issue open for this, but AWS aren't too interested in fixing it: https://github.com/aws-cloudformation/cloudformation-coverage-roadmap/issues/79

In the mean time, you could consider a few of things:

  1. AWS have suggested a workaround using eventbridge: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig-eventbridgeconfig.html#cfn-s3-bucket-eventbridgeconfiguration-eventbridgeenabled
  2. Does the notification really need to be in the CFN template? Could you just add it manually, the same way that the bucket was added?
  3. Can you script the "boring work" using the AWS CLI and some JSON/YAML editors or AWS CDK?
  • Related