How to set Environment Variables on EC2 instance via User Data

前端 未结 9 749
北荒
北荒 2020-12-14 07:34

I am trying to set environment variables with EC2s user data, but nothing i do seems to work

here are the User data scripts i tried

#!/b         


        
9条回答
  •  忘掉有多难
    2020-12-14 08:24

    From this Medium.com article, you can put a script in UserData that writes to a file in /etc/profile.d that will get run automatically when a new shell is run.

    Here is an example cloudformation.yaml

    Parameters:
      SomeOtherResourceData:
        default: Fn::ImportValue: !Sub "SomeExportName"
    Resources:
      WebApi:
        Type: AWS::EC2::Instance
        Properties:
          # ...
          UserData:
            Fn::Base64: !Sub
              - |
                #!/bin/bash
    
                cat > /etc/profile.d/load_env.sh << 'EOF'
    
                export ACCOUNT_ID=${AWS::AccountId}
                export REGION=${AWS::Region}
                export SOME_OTHER_RESOURCE_DATA=${SomeOtherResourceData}
    
                EOF
                chmod a+x /etc/profile.d/load_env.sh
    

    And a YAML that exports something

    # ...
    Outputs:
      SomeExportName:
        Value: !Sub "${WebDb.Endpoint.Address}"
        Export:
          Name: SomeExportName
    

提交回复
热议问题