AWS provides a sample Macro that may help, depending on your use case: https://github.com/awslabs/aws-cloudformation-templates/tree/a11722d/aws/services/CloudFormation/MacrosExamples/S3Objects.
- Create a separate CloudFormation stack with macro.template. This creates the Transform Macro "S3Objects" that will then be available to any other stack in the region. The README.md explains how to "package" the macro template prior to adding to CloudFormation (it will need to include a separate source file for a Lambda function).
- The file example.template provides sample usage. Note the top-level Transform section that refers to
S3Objects
, which allows the use of Type: AWS::S3::Object
.
In the provided example, the Body
property allows for entering text directly in the YAML or JSON template. To expand on that, one can use Fn::Sub to create a file with parameters or properties from other resources:
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: S3Objects
Parameters:
ANameField:
Type: String
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
BucketName: s3bucketuser
VersioningConfiguration:
Status: Enabled
S3Object:
Type: AWS::S3::Object
Properties:
Target:
Bucket: !Ref S3Bucket
Key: README.md
ContentType: text/markdown
Body: !Sub |
# My text file
This is my text file for ${ANameField}.
The region is ${AWS::Region} and my account ID is ${AWS::AccountId}.
This file is in the ${S3Bucket} bucket. The bucket ARN is ${S3Bucket.Arn},
and its website endpoint is ${S3Bucket.WebsiteURL}