Configuring different AWS profiles for Serverless deployment
To deploy our application serverless we need an IAM role that has AWS credentials that consist of AWS_ACCESS_KEY_ID, SECRET_ACCESS_KEY
and other optional fields.
We need to use aws cli
to configure settings related to accounts. Our credentials are stored in ~/.aws/credentials
and are used by the Serverless framework while deploying an application.
There are cases where we need to configure multiple credentials configured to deploy different applications with specific credentials.
Let’s jump onto doing things and you will be able to configure multiple AWS credentials.
Create a new AWS profile:
If you want to create a new AWS profile, you can follow Create an IAM User to create an IAM user in an AWS account and you will get Access key ID and Secret access key.
Now let’s create a new profile in CLI using:
aws configure --profile profileName
- name profileName as per your choice. (This name is what you will be using to deploy)
- You can leave region name and output format empty.
This will create a new profile that you can use now to deploy a serverless application.
Setting profile while deploying:
AWS CLI provides us the option to choose a profile while deploying an application using --aws-profile
with serverless deploy
$> serverles deploy --aws-profile profileName
This application will be deployed using the new profile that we created now.
If you don’t want to specify a profile name every time you are deploying, another option is to specify it in serverless.yml
service: messaging-service // this can be your service nameprovider:
name: aws
stage: test
profile: profileName
Here profile
tells serverless to deploy using the profile specified as value.
Note:
Setting a profile in serverless.yml
is not recommended because if you are working with others then everyone will have to create the same profile locally.
Giving freedom to others to name their profile is better 😉.
Setting profile per stage:
There are some cases where we want to split our profiles for different staging environments to control access to IAM roles.
e.g: If we have a profile by prodAWS
to deploy code in production you would:
$ serverless deploy --stage prod --aws-profile prodAWS
And to deploy to the staging environment you would:
$ serverless deploy --stage dev --aws-profile devAWS
Here, prodAWS
and devAWS
are the AWS profiles for the production and staging environment respectively.
I hope you found this post helpful, please comment below if anyone faces any challenges doing this.
Happy to help and open to suggestions. 😁