Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables using AWS CodeDeploy

Tags:

I have a web application that utilizes environment variables for some of its configuration (DB credentials, API keys, etc). I'm currently using Elastic Beanstalk for deployment and can easily set these from within AWS, which is great because I don't have this sensitive data in my code base.

However, I'm looking into switching from Elastic Beanstalk so I can leverage a bit more flexibility with my web instances, and naturally I'm looking into deploying (from my Codeship CI setup) using CodeDeploy. CodeDeploy is fairly straight forward and I've integrated it with Codeship just fine, but I noticed there's no built-in feature to set environment variables with CodeDeploy like there is with Elastic Beanstalk. Does anyone have any best practices for this process?

like image 971
Jeff Avatar asked Feb 15 '15 06:02

Jeff


People also ask

What environment does the CodeDeploy agent need?

The CodeDeploy agent is required only if you deploy to an EC2/On-Premises compute platform. The agent is not required for deployments that use the Amazon ECS or AWS Lambda compute platform. A configuration file is placed on the instance when the agent is installed. This file is used to specify how the agent works.

How do I set environment variables in CodeBuild?

Choose the icon to edit your CodeBuild action. On the Edit action page, under Environment variables, enter the following: In Name, enter a name for your environment variable. In Value, enter the variable syntax for your pipeline output variable, which includes the namespace assigned to your source action.

What are environment variables in AWS?

An environment variable is a pair of strings that is stored in a function's version-specific configuration. The Lambda runtime makes environment variables available to your code and sets additional environment variables that contain information about the function and invocation request.

What types of applications can be deployed with AWS CodeDeploy?

AWS CodeDeploy can be used for deploying any type of application. To use AWS CodeDeploy, you specify the files to copy and the scripts to run on each instance during the deployment. AWS CodeDeploy is programming language and architecture agnostic, so you can use scripts for any custom deployment logic.


1 Answers

One way I have found to set environment variables is through scripts run during the AfterInstall hook (specified in the appspec http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html).

I am able to determine the environment I am currently deploying to in these scripts by calling to my instances metadata where I get my instance id and then utilize the aws cli to execute describe-tags filtered to my instance Id where I have a tag set for Environment

ID=$(curl "http://169.254.169.254/latest/meta-data/instance-id") aws --region us-east-1 ec2 describe-tags --filters Name=resource-id,Values=$ID Name=key,Values=Environment 

I don't love this, but until Code Deploy has something built in to pass parameters to the appspec, this is the best I can find.

like image 149
Levitron Avatar answered Sep 20 '22 14:09

Levitron