I am implementing a multiregion solution using Cloudformation. Each region will have a number of lambdas, alarms, CW logs, etc. There are some resources that are specific only to one region: cloudfront, global tables, etc.
I decided to create own template file for each region and, as a result, a lot of the code is duplicate. Is there a way to create one template file that could be imported by the other template files for each region?
I did some research and there are modules you could use, but it does not make sense to make a module for each lambda/duplicate resource (we have like 20 of them).
Here’s an example of a lambda that will be duplicated in both regions:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
CodeUri: endpoints
Handler: webhook.handler
Environment:
Variables:
S3_CONTENT_JSON_BUCKET: !Sub "xxxxx"
Layers:
- !FindInMap [ AccountSpecific, !Ref "AWS::AccountId", LoggingArn ]
- !Ref PythonCommonLayer
Policies:
- S3CrudPolicy:
BucketName: !Sub "xxxxx"
Events:
PostContentfulData:
Type: Api
Properties:
RestApiId:
Ref: ApiGateway
Path: /contentful
Method: POST
Auth:
ApiKeyRequired: true```
2
Answers
Sadly no. CloudFormation is region specific, and stacks in one region can’t reference stacks from other regions. However, you can use StackSets to manage mulitple templates across different regions and accounts.
If you do want to use CloudFormation and avoid managing template per region, you can use tools that provides separation between deployment environments (can be defined and configured differently per region) and projects you want to deploy to these environments.
Such tools (Altostra, for example) can generate automatically CloudFormation template according to the environment you are trying to deploy your project to.
This is done on demand, just before deployment execution per environment (aka per region).