Skip to content

Commit

Permalink
feat(core): environment-agnostic cloud assemblies (#2922)
Browse files Browse the repository at this point in the history
Formalize the simple use case for synthesizing cloudformation templates that are not pre-associated with a specific AWS account/region.

When a CDK stack is defined without an explicit `env` configuration, or if `env.account` and/or `env.region` are set to `Aws.accountId`/`Aws.region`, the stack is said to be "environment-agnostic". This means that when a template is synthesized, we will use the CloudFormation intrinsics `AWS::AccountId` and `AWS::Region` instead of concrete account/region.

The cloud assembly manifest for such stacks will indicate `aws://unknown-account/unknown region` to represent that this stack is environment-agnostic, and tooling should rely on external configuration to determine the deployment environment.

Environment-agnostic stacks have limitations. For example, their resources cannot be referenced across accounts or regions, and context providers such as SSM, AZs, VPC and Route53 lookup cannot be used since they won't know which environment to query.

To faciliate the env-agnostic use case at the AWS Construct Library level, this change removes any dependency on concrete environment specification. Namely:

- The AZ provider, which is now accessible through `stack.availabilityZones` will fall back to use `[ Fn::GetAZs[0], Fn::GetAZs[1] ]` in case the stack is env-agnostic. This is a safe fallback since all AWS regions have at least two AZs.
- The use of the SSM context provider by the EC2 and ECS libraries to retrieve AMIs was replaced by deploy-time resolution of SSM parameters, so no fallback is required.

See list of breaking API changes below.

Added a few static methods to `ssm.StringParameter` to make it easier to reference values directly:
* `valueFromLookup` will read a value during synthesis using the SSM context provider.
* `valueForStringParameter` will return a deploy-time resolved value.
* `valueForSecureStringParameter` will return a deploy-time resolved secure string value.

Fixes #2866

BREAKING CHANGE: `ContextProvider` is no longer designed to be extended. Use `ContextProvider.getValue` and `ContextProvider.getKey` as utilities.
* **core:** `Context.getSsmParameter` has been removed. Use `ssm.StringParameter.valueFromLookup`
* **core:** `Context.getAvailabilityZones` has been removed. Use `stack.availabilityZones`
* **core:** `Context.getDefaultAccount` and `getDefaultRegion` have been removed an no longer available.
* **route52:** `HostedZoneProvider` has been removed. Use `HostedZone.fromLookup`.
* **ec2:** `VpcNetworkProvider` has been removed. Use `Vpc.fromLookup`.
* **ec2:** `ec2.MachineImage` will now resolve AMIs from SSM during deployment.
* **ecs:** `ecs.EcsOptimizedAmi` will now resolve AMis from SSM during deployment.
  • Loading branch information
Elad Ben-Israel committed Jun 19, 2019
1 parent c5e43e2 commit c75d245
Show file tree
Hide file tree
Showing 75 changed files with 1,098 additions and 1,174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@
}
}
],
"Throttle": { "RateLimit": 5 },
"Description": "Free tier monthly usage plan",
"Quota": {
"Limit": 10000,
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/test/integ.restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Test extends cdk.Stack {
name: 'Basic',
apiKey: key,
description: 'Free tier monthly usage plan',
throttle: { rateLimit: 5 },
quota: {
limit: 10000,
period: apigateway.Period.Month
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -406,7 +412,9 @@
"FleetLaunchConfig59F79D36": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "FleetInstanceProfileC6192A66"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -580,7 +586,9 @@
"FleetLaunchConfig59F79D36": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "FleetInstanceProfileC6192A66"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -427,7 +433,9 @@
"FleetLaunchConfig59F79D36": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "FleetInstanceProfileC6192A66"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -406,7 +412,9 @@
"FleetLaunchConfig59F79D36": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "FleetInstanceProfileC6192A66"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -559,7 +565,9 @@
"ASGLaunchConfigC00AF12B": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "ASGInstanceProfile0A2834D7"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Parameters": {
"SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
},
"Resources": {
"VPCB9E5F0B4": {
"Type": "AWS::EC2::VPC",
Expand Down Expand Up @@ -406,7 +412,9 @@
"FleetLaunchConfig59F79D36": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-1234",
"ImageId": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamzn2amihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.micro",
"IamInstanceProfile": {
"Ref": "FleetInstanceProfileC6192A66"
Expand Down
Loading

0 comments on commit c75d245

Please sign in to comment.