Cannot run `source` in AWS Codebuild
I am using AWS CodeBuild along with Terraform to automatically deploy a Lambda based service. I have a very simple buildscript.yml
one that does the following:
- Get dependencies
- Running tests
- Get AWS credentials and save to file (see below for details)
- Creds file source
- Launch Terraform
The "source creds file" step is where I am having trouble. I have a simple bash one-liner that grabs AWS containers from curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
and then saves them to a file in the following format:
export AWS_ACCESS_KEY_ID=SOMEACCESSKEY
export AWS_SECRET_ACCESS_KEY=MYSECRETKEY
export AWS_SESSION_TOKEN=MYSESSIONTOKEN
Of course, the obvious step is just source
this file so that these variables can be added to my environment to use Terraform. However, when I do source /path/to/creds_file.txt
, CodeBuild returns:
[Container] 2017/06/28 18:28:26 Running command source /path/to/creds_file.txt
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: source: not found
I tried to install source
via apt
, but then I get an error that source
cannot be found (yes, I ran apt update
, etc.). I am using the default Ubuntu image with Python 2.7 environment for CodeBuild. What can I do to get Terraform worker permissions for the source of this credential file in Codebuild.
Thank!
source to share
AWS CodeBuild images come with a POSIX compliant wrapper. You can see what's inside the images here: https://github.com/aws/aws-codebuild-docker-images .
If you are using specific shell functionality (like source), your best bet is to wrap your commands in a script file with a shebang, specifying the shell you want to run the commands with, and then execute its script from buildspec.yml.
Build-script.sh
#!/bin/bash
<commands>
...
buildspec.yml (snippet)
build:
commands:
- path/to/script/build-script.sh
source to share