The Serverless Framework needs access to your cloud provider's account so that it can create and manage resources on your behalf.
Here we'll provide setup instructions for different cloud provider accounts. Just pick the one for your provider and follow the steps to get everything in place for Serverless.
This guide is for the Amazon Web Services (AWS) provider, so we'll step through the process of setting up credential for AWS and using them with Serverless.
Watch the video on setting up credentials
Here's how to set up the Serverless Framework with your Amazon Web Services account.
If you're new to Amazon Web Services, make sure you put in a credit card.
All AWS users get access to the Free Tier for AWS Lambda. AWS Lambda is part of the non-expiring AWS Free Tier.
If you don't have a credit card set up, you may not be able to deploy your resources and you may run into this error:
AWS Access Key Id needs a subscription for the service
While in the AWS Free Tier, you can build an entire application on AWS Lambda, AWS API Gateway, and more, without getting charged for 1 year... As long as you don't exceed the resources in the free tier, of course.
To let the Serverless Framework access your AWS account, we're going to create an IAM User with Admin access, which can configure the services in your AWS account. This IAM User will have its own set of AWS Access Keys.
Note: In a production environment, we recommend reducing the permissions to the IAM User which the Framework uses. Unfortunately, the Framework's functionality is growing so fast, we can't yet offer you a finite set of permissions it needs (we're working on this). Consider using a separate AWS account in the interim, if you cannot get permission to your organization's primary AWS accounts.
-
Create or login to your Amazon Web Services Account and go to the Identity & Access Management (IAM) page.
-
Click on Users and then Add user. Enter a name in the first field to remind you this User is the Framework, like
serverless-admin
. Enable Programmatic access by clicking the checkbox. Click Next to go through to the Permissions page. Click on Attach existing policies directly. Search for and select AdministratorAccess then click Next: Review. Check everything looks good and click Create user. Later, you can create different IAM Users for different apps and different stages of those apps. That is, if you don't use separate AWS accounts for stages/apps, which is most common. -
View and copy the API Key & Secret to a temporary place. You'll need it in the next step.
You can configure the Serverless Framework to use your AWS API Key & Secret in two ways:
As a quick setup to get started you can export them as environment variables so they would be accessible to Serverless and the AWS SDK in your shell:
export AWS_ACCESS_KEY_ID=<your-key-here>
export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are now available for serverless to use
serverless deploy
# 'export' command is valid only for unix shells. In Windows - use 'set' instead of 'export'
For a more permanent solution you can also set up credentials through AWS profiles. Here are different methods you can use to do so.
Serverless provides a convenient way to configure AWS profiles with the help of the serverless config credentials
command.
Here's an example how you can configure the default
AWS profile:
serverless config credentials --provider aws --key AKIAIOSFODNN7EXAMPLE --secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Take a look at the config
CLI reference for more information about credential configuration.
To set them up through the aws-cli
install it first then run aws configure
to configure the aws-cli and credentials:
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER
Credentials are stored in INI format in ~/.aws/credentials
, which you can edit directly if needed. You can change the path to the credentials file via the AWS_SHARED_CREDENTIALS_FILE environment variable. Read more about that file in the AWS documentation
You can even set up different profiles for different accounts, which can be used by Serverless as well. To specify a default profile to use, you can add a profile
setting to your provider
configuration in serverless.yml
:
service: new-service
provider:
name: aws
runtime: nodejs6.10
stage: dev
profile: devProfile
To easily switch between projects without the need to do aws configure
every time you can use environment variables.
For example you define different profiles in ~/.aws/credentials
[profileName1]
aws_access_key_id=***************
aws_secret_access_key=***************
[profileName2]
aws_access_key_id=***************
aws_secret_access_key=***************
Now you can switch per project (/ API) by executing once when you start your project:
export AWS_PROFILE="profileName2" && export AWS_REGION=eu-west-1
.
in the Terminal. Now everything is set to execute all the serverless
CLI options like sls deploy
.
The AWS region setting is to prevent issues with specific services, so adapt if you need another default region.
You can always specify the profile which should be used via the aws-profile
option like this:
serverless deploy --aws-profile devProfile
As an advanced use-case, you can deploy different stages to different accounts by using different profiles per stage. In order to use different profiles per stage, you must leverage variables and the provider profile setting.
This example serverless.yml
snippet will load the profile depending upon the stage specified in the command line options (or default to 'dev' if unspecified);
service: new-service
provider:
name: aws
runtime: nodejs6.10
stage: ${opt:stage, self:custom.defaultStage}
profile: ${self:custom.profiles.${self:provider.stage}}
custom:
defaultStage: dev
profiles:
dev: devProfile
prod: prodProfile
Be aware! Due to the way AWS IAM and the local environment works, if you invoke your lambda functions locally using the CLI command serverless invoke local -f ...
the IAM role/profile could be (and probably is) different from the one set in the serverless.yml
configuration file.
Thus, most likely, a different set of permissions will be in place, altering the interaction between your lambda functions and others AWS resources.
Please, refer to the invoke local
CLI command documentation for more details.