Reusing CodeBuild by Passing and Overriding Environment Variables from CodePipeline
A story about reusing CodeBuild instead of needlessly creating new ones.
Managing buildspec with Terraform
- buildspec.yml
1 |
|
- codebuild.tf
1 | data "template_file" "buildspec" { |
When you run terraform apply to create the CodeBuild project, the environment variable FOO=foo is set.
Overriding CodeBuild Environment Variables from CodePipeline
This is useful when the CodeBuild processing is the same but you only want to change the environment variables.
1 | resource "aws_codepipeline" "moge" { |
Now when you run CodePipeline, FOO=moge is specified and the value is successfully overridden.
Example
By substituting the nginx ECR repository into ${repository_url}, you can create a CodeBuild project that builds the Nginx image and pushes it to ECR.
1 |
|
By specifying the environment variable REPOSITORY_URL=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/fluentd in CodePipeline, you can build the fluentd image and push it to ECR.
Conclusion
Don’t create CodeBuild projects recklessly.
That’s all.
I hope you find this helpful.
References
I referred to the following for how to specify environment variables.
