How to use SERVERLESS_ACCESS_KEY in shell script

Here is sample shell script

sudo yum install -y nodejs
sudo npm install -g serverless
export SERVERLESS_ACCESS_KEY="my_access_key"

After run script on amazon linux 2023 and setup IAM role for instance.
I can see serverless --version and can write sls.
I can see aws sts get-caller-identity
But when I create project and sls deploy it will have to Login/Register that I don’t want :sob: :sob:

The simplist way is to add the environment variables to the end of ~/.profile

1 Like
sudo yum install -y nodejs
sudo npm install -g serverless
echo "export SERVERLESS_ACCESS_KEY='mykey'" >> ~/.profile
source ~/.profile

I use this script on my Macbook local it work. But When I use this script on EC2 it will not save SERVERLESS_ACCESS_KEY in ~/.profile :sob:

How are you accessing and maintaining that EC2 ?

If you cat ~/.profile does the value/file vanish ?

Over SSH to a basic persistent machine, for instance, this should Just Work.

1 Like

Yes, it’s vanish. :sob: :sob:
I don’t want sls login directly but I want to use Serverless Access Key. When I create sample project on EC2 and try run it shows.
sls deploy --help

:heavy_multiplication_x: Serverless Framework V4 CLI is free for developers and organizations making less than $2 million annually, but requires an account or a license key.

Please login/register or enter your license key: ·

:heavy_multiplication_x: Authentication canceled

So how is that EC2 provisioned ? It seems like there may be some other process or people involved we don’t know about ?
Do you need to use this EC2 for some reason ? Like is this running in a CodePipeline action?

1 Like

Actually, I’m just an intern. My senior asked me to prepare a machine to run the code from Jenkins on the EC2 machine. He asked me to find a way to deploy without use sls login and use only IAM role and Serverless Access key for deploy. I create my own Ec2 on my own account that have IAM policy equal admin. Sorry, there’s something I really don’t know.

Ah ah. So your Jenkins environment probably starts a whole new fresh machine for each build.

You’ll need to find who runs that and have them pass in the new environment variables.

1 Like

You can also add the SERVERLESS_ACCESS_KEY as a part of the deploy command:

SERVERLESS_ACCESS_KEY=valueofkeyhere serverless deploy

1 Like

Finally, I got the answer. I don’t know why save in ~/.profile not work but in ~/.bashrc works! Thank you for suggest me. It helps a lot. Sorry for reply late cause I have a lot thing to do :cry: When running my t2.micro EC2 it should had to waited 9 mins for save serverless_key to bashrc

sudo npm install -g serverless
echo "export SERVERLESS_ACCESS_KEY='samr4jr94jf9f9n3nvv'" >> /home/ec2-user/.bashrc
source /home/ec2-user/.bashrc
serverless_access_key='samr4jr94jf9f9n3nvv'
export SERVERLESS_ACCESS_KEY="$serverless_access_key"
echo $SERVERLESS_ACCESS_KEY