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

feat(core): automatic cross stack, cross region references (under feature flag) #22008

Merged
merged 37 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7f47baa
feat(core): automatic cross stack, cross region references
corymhall Sep 12, 2022
4ce8290
fixing build
corymhall Sep 13, 2022
97416cc
adding some tests and integration tests
corymhall Sep 14, 2022
cfcdd08
fixing integration tests
corymhall Sep 14, 2022
7a31868
hopefully fixing integ tests
corymhall Sep 14, 2022
b54bd80
Merge branch 'main' into corymhall/cross-region-provider
corymhall Sep 14, 2022
80f4092
updating README
corymhall Sep 15, 2022
783cb02
switching to ssm writer
corymhall Sep 23, 2022
104a249
Merge remote-tracking branch 'upstream/main' into corymhall/cross-reg…
corymhall Sep 23, 2022
49944e6
updating integration tests
corymhall Sep 23, 2022
a4ac262
renaming
corymhall Sep 23, 2022
3f99d97
updating docs
corymhall Sep 23, 2022
13b831b
fixing build
corymhall Sep 23, 2022
38c4226
adding stack name to ssm path prefix
corymhall Sep 23, 2022
56b4fe6
refactoring provider to use OldResourceProperties
corymhall Sep 26, 2022
5160273
rerunning integ tests
corymhall Sep 27, 2022
1933290
fixing formatting
corymhall Sep 27, 2022
472c337
fixing tests
corymhall Sep 27, 2022
308cb1b
Adding export reader in consuming stack
corymhall Sep 30, 2022
b02046d
updating docs
corymhall Sep 30, 2022
b833df2
fixing integ tests
corymhall Sep 30, 2022
954e005
Merge branch 'main' into corymhall/cross-region-provider
corymhall Sep 30, 2022
a58b1e7
adding another integ tests for cloudfront cross region acm certificates
corymhall Sep 30, 2022
d702e0f
fixing formatting
corymhall Sep 30, 2022
f9cbab7
fixing version of aws-sdk
corymhall Oct 3, 2022
3a1c86b
fix test
corymhall Oct 3, 2022
38b6779
fixing formatting in integ test
corymhall Oct 3, 2022
2c1a37b
Merge branch 'main' into corymhall/cross-region-provider
Naumel Oct 12, 2022
500ea0c
Merge branch 'main' into corymhall/cross-region-provider
Naumel Oct 13, 2022
7ffd80b
updates based on review comments
corymhall Oct 13, 2022
5fe1d7b
updating integ-tests and readme
corymhall Oct 13, 2022
01f3366
updating based on review comments
corymhall Oct 20, 2022
9e1c2dc
fixing tests
corymhall Oct 20, 2022
6b9660d
updating docs to indicate that this feature is experimental
corymhall Oct 28, 2022
fdea663
removing `optIn` from property name
corymhall Oct 31, 2022
3c1f2ff
Merge branch 'main' into corymhall/cross-region-provider
mergify[bot] Oct 31, 2022
6197448
Merge branch 'main' into corymhall/cross-region-provider
mergify[bot] Oct 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudformation/package.json
Expand Up @@ -85,6 +85,7 @@
"@aws-cdk/aws-ssm": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/integ-tests": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/aws-lambda": "^8.10.104",
Expand Down

Large diffs are not rendered by default.

@@ -0,0 +1 @@
export declare function handler(event: AWSLambda.CloudFormationCustomResourceEvent): Promise<void>;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,98 @@
/*eslint-disable no-console*/
/* eslint-disable import/no-extraneous-dependencies */
import { SSM } from 'aws-sdk';
const SSM_EXPORT_PATH = '/cdk/exports/';
type CrossRegionExports = { [exportName: string]: string };

export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) {
const props = event.ResourceProperties;
const exports: CrossRegionExports = props.Exports;

const ssm = new SSM({ region: props.Region });
try {
switch (event.RequestType) {
case 'Create':
console.info(`Creating new SSM Parameter exports in region ${props.Region}`);
await putParameters(ssm, exports);
return;
case 'Update':
console.info(`Reading existing SSM Parameter exports in region ${props.Region}`);
const existing = await getExistingParameters(ssm);
const paramsToDelete = returnMissing(existing, exports);
console.info(`Deleting unused SSM Parameter exports in region ${props.Region}`);
if (paramsToDelete.length > 0) {
await ssm.deleteParameters({
Names: paramsToDelete,
}).promise();
}
console.info(`Creating new SSM Parameter exports in region ${props.Region}`);
await putParameters(ssm, exports);
return;
case 'Delete':
console.info(`Reading existing SSM Parameter exports in region ${props.Region}`);
const existingParams = await getExistingParameters(ssm);
console.info(`Deleting all SSM Parameter exports in region ${props.Region}`);
await ssm.deleteParameters({
Names: Array.from(Object.keys(existingParams)),
}).promise();
return;
default:
return;
}
} catch (e) {
console.error('Error processing event: ', e);
throw e;
}
};

/**
* Create parameters for existing exports
*/
async function putParameters(ssm: SSM, parameters: CrossRegionExports): Promise<void> {
await Promise.all(Array.from(Object.entries(parameters), ([name, value]) => {
return ssm.putParameter({
Name: `${SSM_EXPORT_PATH}${name}`,
Value: value,
Type: 'String',
}).promise();
}));
}

function returnMissing(a: CrossRegionExports, b: CrossRegionExports): string[] {
const missing: string[] = [];
for (const name of Object.keys(a)) {
if (!b.hasOwnProperty(name)) {
missing.push(name);
}
}
return missing;
}

/**
* Get existing exports from SSM parameters
*/
async function getExistingParameters(ssm: SSM): Promise<CrossRegionExports> {
const existingExports: CrossRegionExports = {};
function recordParameters(parameters: SSM.ParameterList) {
parameters.forEach(param => {
if (param.Name && param.Value) {
existingExports[param.Name] = param.Value;
}
});
}
const res = await ssm.getParametersByPath({
Path: `${SSM_EXPORT_PATH}`,
}).promise();
recordParameters(res.Parameters ?? []);

while (res.NextToken) {
const nextRes = await ssm.getParametersByPath({
Path: `${SSM_EXPORT_PATH}`,
NextToken: res.NextToken,
}).promise();
recordParameters(nextRes.Parameters ?? []);
res.NextToken = nextRes.NextToken;
}
return existingExports;
}

@@ -0,0 +1 @@
{"version":"21.0.0"}
@@ -0,0 +1,34 @@
{
"version": "21.0.0",
"files": {
"61c5467a13581c441143dacb7d8878bb691678bb777668c0d4bbf28d05538404": {
"source": {
"path": "crossregionconsumerIntegNested815BEF8A.nested.template.json",
"packaging": "file"
},
"destinations": {
"current_account-us-east-2": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-2",
"objectKey": "61c5467a13581c441143dacb7d8878bb691678bb777668c0d4bbf28d05538404.json",
"region": "us-east-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-us-east-2"
}
}
},
"073479a1920f44222959fc9231ad62d389ab3fe14dc0a6cdc14a3f1f54fb8dfc": {
"source": {
"path": "cross-region-consumer.template.json",
"packaging": "file"
},
"destinations": {
"current_account-us-east-2": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-2",
"objectKey": "073479a1920f44222959fc9231ad62d389ab3fe14dc0a6cdc14a3f1f54fb8dfc.json",
"region": "us-east-2",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-us-east-2"
}
}
}
},
"dockerImages": {}
}
@@ -0,0 +1,77 @@
{
"Resources": {
"IntegNestedNestedStackIntegNestedNestedStackResource168C5881": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": {
"Fn::Join": [
"",
[
"https://s3.us-east-2.",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-2"
},
"/61c5467a13581c441143dacb7d8878bb691678bb777668c0d4bbf28d05538404.json"
]
]
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"IntegParameter02A1817A4": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Type": "String",
"Value": "{{resolve:ssm:/cdk/exports/cross-region-producer-IntegQueueFnGetAttIntegQueue3A18718AQueueName6E52E429}}",
"Name": "integ-parameter0"
}
},
"IntegParameter1EDBEF1C6": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Type": "String",
"Value": "{{resolve:ssm:/cdk/exports/cross-region-producer-IntegNestedNestedStackIntegNestedNestedStackResourceFnGetAttIntegNestedNestedStackIntegNestedNestedStackResource168C5881OutputscrossregionproducerIntegNestedNestedIntegQueueD686DB69QueueNameC3296698}}",
"Name": "integ-parameter1"
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}