# How to use SERVERLESS\_ACCESS\_KEY in shell script

**URL:** <https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564>\
**Category:** Serverless Framework\
**Tags:** aws, lambda, variables, iam\
**Created:** [September 19, 2024, 2:58am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564 "2024-09-19T02:58:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Potterpurpp](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/potterpurpp/32/8058_2.png) [@Potterpurpp](https://forum.serverless.com/u/Potterpurpp)\
**Post date:** [September 19, 2024, 2:58am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/1 "2024-09-19T02:58:19Z")

</div>

Here is sample shell script

```auto
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 😭 😭

---

<div class="post-metadata">

**Author:** ![TomC](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tomc/32/4002_2.png) [@TomC](https://forum.serverless.com/u/TomC)\
**Post date:** [September 19, 2024, 7:16am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/2 "2024-09-19T07:16:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Potterpurpp](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/potterpurpp/32/8058_2.png) [@Potterpurpp](https://forum.serverless.com/u/Potterpurpp)\
**Post date:** [September 19, 2024, 8:05am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/3 "2024-09-19T08:05:10Z")

</div>

```auto
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 😭

---

<div class="post-metadata">

**Author:** ![TomC](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tomc/32/4002_2.png) [@TomC](https://forum.serverless.com/u/TomC)\
**Post date:** [September 19, 2024, 8:16am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/4 "2024-09-19T08:16:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Potterpurpp](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/potterpurpp/32/8058_2.png) [@Potterpurpp](https://forum.serverless.com/u/Potterpurpp)\
**Post date:** [September 19, 2024, 8:26am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/5 "2024-09-19T08:26:26Z")

</div>

Yes, it’s vanish. 😭 😭  
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`

✖ 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: ·

✖ Authentication canceled

---

<div class="post-metadata">

**Author:** ![TomC](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tomc/32/4002_2.png) [@TomC](https://forum.serverless.com/u/TomC)\
**Post date:** [September 19, 2024, 8:43am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/6 "2024-09-19T08:43:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Potterpurpp](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/potterpurpp/32/8058_2.png) [@Potterpurpp](https://forum.serverless.com/u/Potterpurpp)\
**Post date:** [September 19, 2024, 8:51am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/7 "2024-09-19T08:51:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TomC](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tomc/32/4002_2.png) [@TomC](https://forum.serverless.com/u/TomC)\
**Post date:** [September 19, 2024, 12:27pm UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/8 "2024-09-19T12:27:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![garethmcc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/garethmcc/32/2276_2.png) [@garethmcc](https://forum.serverless.com/u/garethmcc)\
**Post date:** [September 24, 2024, 3:29pm UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/9 "2024-09-24T15:29:56Z")

</div>

You can also add the SERVERLESS\_ACCESS\_KEY as a part of the deploy command:

`SERVERLESS_ACCESS_KEY=valueofkeyhere serverless deploy`

---

<div class="post-metadata">

**Author:** ![Potterpurpp](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/potterpurpp/32/8058_2.png) [@Potterpurpp](https://forum.serverless.com/u/Potterpurpp)\
**Post date:** [September 30, 2024, 7:53am UTC](https://forum.serverless.com/t/how-to-use-serverless-access-key-in-shell-script/20564/10 "2024-09-30T07:53:13Z")

</div>

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 😢 When running my t2.micro EC2 it should had to waited 9 mins for save serverless\_key to bashrc

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

```
