How Do You Add Environment Variables to CodeBuild buildspec.yml?


I’m trying to use a CloudFormation template to define CodeBuild and CodePipeline to automate the deployment of a static website hosted in an S3 bucket. To give credit where credit’s due, I’m largely following the template from

The problem I can’t resolve is that after I add an environment variable for the Hugo version I’d like to use to create the static site files, I get an error from the AWS console that reads: “Template validation error: Template format error: Unresolved resource dependencies [HUGO_VERSION] in the Resources block of the template.”

Why isn’t it accepting the HUGO_VERSION environment variable that I define under environment_variables? This is version 0.1 of the format, so it’s a little different than the current 0.2, but I’ve read the following link:

The thing that really confuses me is that if I remove the lines with ${HUGO_VERSION}, the template is accepted just fine – and then inspection of the CloudWatch logs after building shows (because of the printenv command) HUGO_VERSION=0.49! What gives?

Originally, the template looks like this.

Now after I try to add an environment variable to use Hugo in the pipeline, the template looks like this.

EDIT 10/20

Still haven’t solved this. I tried to follow the advice given below by matsev, but I’m still getting the same validation error. For completeness, the latest template I’m trying is


Please check the Environment property of the AWS::CodeBuild::Project in your CloudFormation template. Specifically the EnvironmentVariables allows you to specify environment variables, e.g.

Now in you can reference the HUGO_VERSION as an environment variable in your buildspec.yml file, e.g.

Leave a Reply