This project contains source code and supporting files for a CloudFormation Macro that you can deploy from the Serverless Application Repository or with the SAM CLI. It includes the following files and folders:
- src - Code for the macro's Lambda function.
- src/tests - Unit tests for the macro code.
- events - Invocation events that you can use to invoke the function.
- template.yaml - A template that defines the macro's AWS resources.
The template defines several AWS resources:
- A Lambda function
- A CloudWatch Log group for the Lambda Function
- A CloudFormation Macro
- A CloudWatch Log group for the CloudFormation Macro
The CloudFormation Macro can be deployed using the AWS Console Serverless Application Repository or the AWS SAM CLI.
Macros can only be used from the same account they are deployed to. Ensure you deploy the macro to the account and regions you plan to deploy your Buildkite ECS Task Definitions in.
Open the application in AWS Console and click Deploy, fill in the parameters as described below for a CLI deployment.
First, install the SAM CLI if not already installed. The following instructions were written using SAM version 0.46.2.
To deploy the macro for the first time, authenticate with AWS and run the following command from this directory:
transform$ sam deploy --guided
You will be prompted for the following parameter values:
- Stack Name: The name of the stack to deploy to CloudFormation. This must
be unique to your account and region, use something descriptive like
buildkite-on-demand-macro
. - AWS Region: The AWS region you want to deploy the macro to inside your account. Deploy the transform to all regions you plan to deploy Buildkite ECS Task Definitions to.
- Parameter TransformName: The transform name is how you will invoke the transform using the CloudFormation Transform section in your agent template. You should use the default transform name unless you are developing a fork of the transform.
- Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
- Allow SAM CLI IAM role creation: IAM role creation is not required but passing no prompts you for Capabilities which doesn't support a blank value 🤷 so say yes for now.
- Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run
sam deploy
without parameters to deploy changes to your application.
Build the Lambda package with the sam build
command.
transform$ sam build
The SAM CLI installs dependencies defined in src/package.json
, creates a
deployment package, and saves it in the .aws-sam/build
folder.
Test a the macro function by invoking it directly with a test event. An event is
a JSON document that represents the input that the function receives from the
event source. Test events are included in the events
folder in this project.
Run functions locally and invoke them with the sam local invoke
command.
transform$ sam local invoke TransformFunction --event events/event.json
To simplify troubleshooting, SAM CLI has a command called sam logs
. sam logs
lets you fetch logs generated by your deployed Lambda function from the command
line. In addition to printing the logs on the terminal, this command has several
nifty features to help you quickly find the bug.
NOTE
: This command works for all AWS Lambda functions; not just the ones you
deploy using SAM.
transform$ sam logs -n TransformFunction --stack-name buildkite-on-demand-transform --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
Tests are defined in the src/tests
folder in this project. Use NPM to install
the Mocha test framework and run unit tests.
transform$ cd src
src$ npm install
src$ npm run test
To delete the sample application that you created, use the AWS CLI. Assuming you used the suggested stack name, you can run the following:
aws cloudformation delete-stack --stack-name buildkite-on-demand-macro
See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.