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
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