Add your AWS API key info in a Key Vault for Terraform
EDIT: Updated on July 10, 2019; modified second- and third-last paragraphs to show the correct process of retrieving the AWS_SECRET_ACCESS_KEY from the Key Vault and setting it as a protected environment variable
Our primary cloud is in Azure which makes building DevOps pipelines with automation scoped to a particular subscription very easy, but what happens when we want to deploy something in AWS, since storing keys in source control is A Very Bad Idea™?
Simple, we use Azure Key Vault.
First, we created a Key Vault specifically for this purpose called company-terraform
which will specifically be used to store the various secrets for Terraform-based deployments. When you tie a subscription from Azure DevOps to an Azure subscription, it creates an “application” in the Azure Enterprise Applications list, so give that application Get and List permissions to this vault.
Next, we created a secret called AmazonAPISecretKey
and then set the secret’s content to the actual API key you are presented when you enable programmatic access to an account in the AWS IAM console.
In our Azure DevOps Terraform build and release pipelines, we then added an Azure Key Vault step, selecting the appropriate subscription and Key Vault. Once selected, we added a Secrets filter AmazonAPISecretKey
meaning that it will only ever fetch that secret on run; if you will be adding multiple secrets which will all be used in this particular pipeline, add them to this filter list.
Finally, we can now use the string $(AmazonAPISecretKey)
in any shellexec or other pipeline task to authenticate against AWS, while never having to commit the actual key to a viewable source.
Since one of the methods the Terraform AWS provider can use to authenticate is by using the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables, we will set them up so that DevOps can use them in its various tasks.
First, open your Build or Release pipeline and select the Variables tab. Create a new variable called AWS_ACCESS_KEY_ID
and set the value to your access key ID (usually something like AK49FKF4034F42DZV2VRMD
). Then create a second variable called AWS_SECRET_ACCESS_KEY
which you can leave blank, but click the padlock icon next to it, to tell DevOps that its contents are secret and shouldn’t be shared.
Now create a shellexec
task and add the following command to it, which will set the AWS_SECRET_ACCESS_KEY
environment variable to the contents of the Key Vault entry we created earlier:
echo "##vso[task.setvariable variable=AWS_SECRET_ACCESS_KEY;]$(AmazonAPISecretKey)"
And there you have it! You can now reference your AWS accounts from within your Terraform structure without ever actually exposing your keys to prying eyes!