Dynamize (aka variablize) your AWS deployment account using the Serverless framework

Last time we dynamized your serverless resources based on your deployment stage. Now we'll dynamize your AWS deployment account! Yes, that's right, automation all the way!

Dynamize (aka variablize) your AWS deployment account using the Serverless framework

This will be short. Remember how we dynamized our serverless resources' parameters? How I said at the beginning that we definitely should use different accounts to at least separate production and development environments? Well, the Serverless framework has profile parameter.

This parameter defines which profile from your ~/.aws/crendentials file will be used to deploy your service. So yes, this means we can dynamize that too! How? Just like we did for the DynamoDB retain policy!

Let's get our previous post's sources.

Yes, we are building your very own highly automated control room - Photo by Patryk Grądys / Unsplash

Setup your AWS accounts

First, setup your aws accounts. It's straightforward; just follow this guide.

Dynamize your serverless.yml profile

Then, add a profiles property to your custom properties, where we'll define the different aws profiles you'll use for your different stages

custom:
    profiles:
        prod: parsiweb-prod
        staging: parsiweb-staging
        other: parsiweb-dev

Next, set the profile property in your serverless.yml's provider, dynamized based on the stage, like this

provider`
    name: aws
    runtime: nodejs8.10
    stage: ${opt:stage, 'dev'}
    profile: ${self:custom.profiles.${self:provider.stage}, self:custom.profiles.other}

Deploy!

‌Deploy on the different stages we defined previously (sls deploy --stage your_stage, sls deploy --stage prod), and remember to both keep the stage short as to not have the deployment fail because of an excessive roleName length and remove the deployed services afterward.

That's it! Have a great day!