Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple AWS credentials per workflow #6

Open
j opened this issue Jan 11, 2019 · 8 comments
Open

Multiple AWS credentials per workflow #6

j opened this issue Jan 11, 2019 · 8 comments

Comments

@j
Copy link

j commented Jan 11, 2019

How can I specify two different aws credentials (accounts) for a build? I want to be able to deploy to separate aws accounts (staging / production).

@tj
Copy link
Member

tj commented Jan 12, 2019

I think you'd have to filter on the branches you want, then create two different deployment actions in order to tweak config

@j
Copy link
Author

j commented Jan 12, 2019

Yeah, I was going to go this route. I wasn't sure how multiple workflow files worked. I wish when you filter, you can choose to close the entire workflow execution or just cancel the branch, so I can start with a filter and continue with a branch.

@j j closed this as completed Jan 12, 2019
@j j reopened this Jan 12, 2019
@j
Copy link
Author

j commented Jan 12, 2019

I'm going through this, and creating a new workflow and using "AWS_ACCESS_KEY_ID" or "AWS_SECRET_ACCESS_KEY" secrets don't let me reset the value. Secrets are persisted for the entire action area... :(

@tj
Copy link
Member

tj commented Jan 12, 2019

ah damn, that's kind of lame, otherwise yeah you could have two concurrent filters for the two branches and do it that way, hmm... haha

@j
Copy link
Author

j commented Jan 14, 2019

@tj exactly. actions is pretty amazing, they still have a long way to go. Even if I could do "secrets per branch" type of thing, that'd be cool.

I finally got a successful SAM + "staging" branch deploy, so I'm going to test a production one today and hope to knock it out.

My current solution is storing the AWS credentials file as a secret "AWS_CREDENTIALS", running a plain bash action to create a .aws/credentials file within my repository, and setting the SAM to use it.

action "create aws credentials" {
  uses = "actions/bin/sh@master"
  needs = "filter staging branch"
  secrets = ["AWS_CREDENTIALS"]
  args = ["rm -rf ${GITHUB_WORKSPACE}/.aws && mkdir ./.aws && echo \"$AWS_CREDENTIALS\" >> ${GITHUB_WORKSPACE}/.aws/credentials"]
}

action "staging sam package" {
  uses = "apex/actions/aws/sam@master"
  needs = "create aws credentials"
  args = "package --profile example-${NODE_ENV} --template-file template.yml --output-template-file template-packaged.yml --s3-bucket example-${NODE_ENV}"
  env = {
    NODE_ENV = "staging"
    AWS_SHARED_CREDENTIALS_FILE = "${GITHUB_WORKSPACE}/.aws/credentials"
  }
}

This way worked, just need to copy paste (😭) and change things to production. I was hoping I could create an action and dynamically set the NODE_ENV variable to have it all be non-copy/pasta, but it doesn't save env variables throughout.

@pavan-shipmnts
Copy link

@j did creating two action workflows work for you?
I'm still confused on how to load a separate AwS profile for prod and staging.

@tj
Copy link
Member

tj commented Aug 3, 2020

@pavan-shipmnts its been a while now, I forget haha, I think it worked but I don't use GH Actions since I work solo, there might be a better way to approach environments now that GH Actions is more mature

@gerjunior
Copy link

gerjunior commented Mar 25, 2021

You can use the AWS CLI to create multiple profiles:

- name: Set AWS Credentials
        run: |
          aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} --profile firstProfile
          aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} --profile firstProfile
          aws configure set region ${ secrets.AWS_DEFAULT_REGION} --profile firstProfile
          aws_assume=($(aws sts assume-role \
              --role-arn "arn:aws:iam::${accountId}:role/${roleName}" \
              --role-session-name "${roleSessionName}" \
              --profile firstProfile \
              --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
              --output text))
          aws configure set aws_access_key_id "${aws_assume[0]}" --profile secondProfile
          aws configure set aws_secret_access_key "${aws_assume[1]}"  --profile secondProfile
          aws configure set aws_session_token "${aws_assume[2]}"  --profile secondProfile

You can use the --profile ${name} flag in basically every aws command. After your set those credentials, just pass the flag with the name you want for each command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants