Set Multiline Environment Variables In Elastic Beanstalk With Terraform

Solomon Okwa
Nov 5 · 2 min read
AWS Elastic Beanstalk and HashiCorp Terraform

AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.

With the benefits of Continuous Integration/Continuous Deployments, adopting Infrastructure as Code(IaC) bring lots of benefits.

I was stuck for a few hours trying to set a multiline environment variable specifically an RSA private key for an Elastic Beanstalk deployment because the newline character (\n) was either an illegal character or not allowed.

Solution

The solution was to base64 encode the value because the base64 encoded string contains only ASCII characters. The fix required a little change in my code which was to decode the base64 encoded value before use.

To achieve this with terraform was simple. I used the base64encode function to achieve it:

Terraform HCL

Conclusion

You can adopt this same idea of base64 encoding values for other cloud providers when you have issues setting values with special characters.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade