EC2 and Secret Manager

0

Hi, I am carrying out a POC to save my docker-compose .env secrets in AWS secret manager and retrieve the secret into my application running on an EC2 instance in an autoscaling group and another app running in Elastic beanstalk. I have created an Instance profile with IAM policy to retrieve the secrets from the secret manager using the secret arn. But each time I deploy this application, the docker-compose is not able to access the secrets. When I inspect the container I see that it was not able to get the secrets. How do I do this?

질문됨 일 년 전703회 조회
1개 답변
3
수락된 답변

Please review the following link. To access the secrets stored in AWS Secrets Manager from your application running on EC2 instances, you can use AWS SDKs or CLI commands.

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/secrets-manager.html

Update your docker-compose.yml file to use the .env file:

services:
  your-service:
    image: your-image
    env_file:
      - .env

python fetch_secrets.py and produce .env file then run docker-compose

docker-compose up

For applications running on Elastic Beanstalk Create a .ebextensions directory in your application's root directory Create a config file, for example, 01_fetch_secrets.config, inside the .ebextensions directory with the following content

container_commands:
  01_fetch_secrets:
    command: "python fetch_secrets.py"

Include the fetch_secrets.py script in your application's source code Deploy your application to Elastic Beanstalk. During deployment, Elastic Beanstalk will execute the fetch_secrets.py script and create the .env file. Your application should now be able to access the secrets stored in the .env file.

profile picture
전문가
답변함 일 년 전
  • Thanks for this. I will implement this and give feedback of the outcome.

  • Thanks, a lot this worked for me. Though what I did differently was use the AWS SDK for Node JS, and wrote a function that gets secret from Parameter store and append it as env for the application. I believe the same implementation should work for secret manager. I went the parameter store option. I saved the secret as Json on parameter store and retrieve via the function.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠