aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Make the CLI experience of multi-account CDK apps awesome

Open skinny85 opened this issue 7 years ago • 33 comments

After all of the issues/PRs for cross-account CodePipelines are delivered (#3208, #3323 , #3387, #3388, #3389 ), we will have a great story for making it very easy to work with cross-account CodePipelines.

However, we also need to make it easy to work with cross-account CDK apps from the perspective of authentication from the command line. Most likely, this will involve a deeper integration with AWS profiles defined in the ~/.aws/credentials / ~/.aws/config files. A possible solution would be to allow passing profile when creating a Stack:

new Stack(app, 'Stack1', {
  env: {
     profile: 'account1',
  },
});

The ideal customer experience we want here is to be able to say:

$ cdk deploy '*'

, and all of the different credentials for the different accounts the Stacks belong to will be automatically wired together.

skinny85 avatar Jul 23 '19 18:07 skinny85

An interesting point: the CloudFormation cross-account actions defined in #3208 create a new stack that contains the IAM roles needed to deploy into the target account. How do we want to model authorization for that use-case? Maybe passing the profile in Stack's env like above is not a great idea; perhaps we need some sort of configurable account-to-profile mapping instead?

skinny85 avatar Aug 03 '19 00:08 skinny85

(Probably an option to specify the CloudFormation role ARN should also be added.)

tader avatar Aug 05 '19 11:08 tader

(Probably an option to specify the CloudFormation role ARN should also be added.)

That's already possible (you can pass a role when creating the action).

skinny85 avatar Aug 05 '19 19:08 skinny85

I don’t think it is in the context of this issue, i.e., using profiles for various accounts. Each account would require its own CF role.

(Probably an option to specify the CloudFormation role ARN should also be added.)

That's already possible (you can pass a role when creating the action).

tader avatar Aug 05 '19 20:08 tader

I'm pretty sure the credentials that the profiles include refer to an IAM user or role already, so I'm not sure what "specifying a role" would mean in this context.

skinny85 avatar Aug 05 '19 20:08 skinny85

As a developer I could only have the permission to start a CloudFormation deploy. I can tell CloudFormation to assume an other role with elevated permissions. See the —role-arn option of the cdk command

I'm pretty sure the credentials that the profiles include refer to an IAM user or role already, so I'm not sure what "specifying a role" would mean in this context.

tader avatar Aug 05 '19 20:08 tader

Ah, got it. Thanks for the explanation.

Yes, that's something that this feature should definitely include.

skinny85 avatar Aug 05 '19 23:08 skinny85

It could be as easy as specifying an optional .aws/credentials profile in the Environment, in addition to region and account.

slotnick avatar Aug 23 '19 23:08 slotnick

Is the wildcard option truly working?

cdk deploy * -c stage=dev
No stack found matching 'README.md'. Use "list" to print manifest

Seems to do this no matter what I do I only tried wildcards cause the cdk deploy command mentions it.

btotharye avatar Sep 05 '19 16:09 btotharye

Yes, this is a common gotcha. The "No stack found matching 'README.md'" should be a hint :).

The * is expanded by your shell, by default, to a list of all files in the current directory. To pass the * into the CDK, you need to escape it; so either:

cdk deploy \* -c stage=dev

or

cdk deploy '*' -c stage=dev

skinny85 avatar Sep 05 '19 17:09 skinny85

Yes, this is a common gotcha. The "No stack found matching 'README.md'" should be a hint :).

The * is expanded by your shell, by default, to a list of all files in the current directory. To pass the * into the CDK, you need to escape it; so either:

cdk deploy \* -c stage=dev

or

cdk deploy '*' -c stage=dev

ha thanks good to know

btotharye avatar Sep 05 '19 17:09 btotharye

Thanks for this. Will this also fix the detection for non existing circular references like #3300 ?

tijoer avatar Sep 09 '19 15:09 tijoer

I am curious what people think about this idea which came up as part of the CI/Cd design: My current thinking is that when you bootstrap an environment using the cdk you will be able to specify a trusted account that will be able to deploy into this environment and then the cli will just assume a role in the target and deploy into it. No profiles needed...

eladb avatar Sep 10 '19 16:09 eladb

There will be two scenarios where CDK is used:

  • Developers deploying infrastructure
  • Pipelines deploying infrastructure

If we can attach a policy to pipelines that sufficient rights are granted everything would be fine. The same should go for developers. Let me define a role that allows them to execute cloudformation in each account and make the generation of cloudformation possible without cyclic reference checking.

Using profiles would be a pain in the long way, as I already have three different entries in my ~/.aws/credentials file for some accounts right now and the other developers have named their credentials differently, however we try to enforce a common naming scheme right now. I never liked profiles and they feel kinda off, but I can understand why they are needed.

env: {
  account: '123456789',
}

should be better for tooling then

env: {
     profile: 'account1', //might change for each developer
}

tijoer avatar Sep 11 '19 07:09 tijoer

I wouldn't assume too much about how the CDK is used, tends to force people into patterns that might not apply to them. Defining a trusted account sounds great as an option and applies to many use cases I can think of, but it may be a pain in more restricted environments, where whoever does the deploying doesn't have enough access to also establish a trust between accounts. I think that the original idea of deeper integration with existing AWS authentication mechanism is right, because it's in line with how other AWS tools work, like aws-cli and the SDKs.

Maybe the CDK needs a local configuration file/option that isn't packaged with the app. One that is specific to whoever is currently using the CDK. Similar to profiles, actually. It could even be stored in the same directory, ~/.aws/cdk. And in that file the user could say "when deploying or synthesizing for this account, use this profile, or these credentials. also, define these account-specific variables, which are like cloudformation parameters".

This is almost possible now. What I currently do is use context variables, from cdk.json and command line. In cdk.json I have a structure like:

{
  "accounts":{
    "account_id":"12345",
    "regions":{
      "region":"eu-west-1",
      "environments":[
        {
          "env":"staging",
          "load_balancer":"arn:aws:etc",
          "subnets":[
            "snet-1234",
            "snet-5432"
          ]
        },
        {
          "env":"production",
          "load_balancer":"arn:aws:etc",
          "subnets":[
            "snet-7890",
            "snet-0987"
          ]
        }
      ]
    }
  }
}

And then in code I create the stack by looping through the above, like:

for environ in environments:
  stack = Stack(app, 'MyStack-' + environ)

I also mix SDK and CDK (because in Python it's easy) and need to know in code which profile is in use, so I run the CDK with:

cdk --profile myprofile --context profile=myprofile destroy ecs-prd

But:

  • I can't use multiple profiles in the same command, so I need to run the cdk once for each account. Which I do manually, but it could be scripted.
  • cdk.json gets stored in version control. In some cases it's exactly what I want, because I'm working on an app that is specific to this company/team/environment. In other cases I might want to package the app for some team that uses completely different accounts, so I don't want these defined in cdk.json.

Because I mix the CDK and SDK in the same code, I'd like the ability to pass the credentials used by the CDK to the SDK. But that can be worked around.

Because of the second point above, I'm thinking that maybe ~/.aws/cdk should have the same format as cdk.json and if the same variable is defined in both, then ~/aws/cdk should take precedence and overwrite what's in cdk.json. In which case the local file should probably be named ~/.aws/cdk.json.

Again, the option of defining a trusted account and sidestepping all this personal configuration sounds great, as long as it's not the only option.

bgdnlp avatar Sep 13 '19 06:09 bgdnlp

Commenting to register my support in making multi-account support better. I'm also interested in seeing how this could be used to support cellular architectures, where new accounts can be spun up, on the fly, using a service like AWS Organizations.

I suspect that designing this feature around supporting cellular accounts might have a rising-tide effect for less demanding use-cases.

davidbarsky avatar Sep 22 '19 18:09 davidbarsky

Can the example from the aws-events overview section on Cross-account targets actually be run from the command line using cdk CLI? I'm trying to do something similar to this with cross-account targets and I don't know how to access two different accounts in a single pass. This feature is to make the experience awesome. I'd be OK right now with any experience that works.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-events-readme.html#cross-account-targets

gsdwait avatar Jan 10 '20 02:01 gsdwait

I came here with exactly the same question as @gsdwait

Any info?

saltman424 avatar Jan 28 '20 15:01 saltman424

I also face the challenge, in organisation we centalize the logs,notification and alarms. Setup in one shot the tooling for client and admin account will ease a lot the deployement. Congrats for the CDK tool by the way, infra as an app!

faridux avatar Jan 28 '20 19:01 faridux

@saltman424 @faridux We solved it with a plugin: https://github.com/hupe1980/cdk-multi-profile-plugin

hupe1980 avatar Jan 28 '20 19:01 hupe1980