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

fix: ensure all generated logical ids conform to CloudFormation standard #396

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/constructs/core/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ describe("AppIdentity.suffixText", () => {
expect(actual).toEqual(expected);
});

it("should handle hyphens", () => {
it("should handle non-alphanumeric characters (e.g. hyphens)", () => {
const actual = AppIdentity.suffixText({ app: "my-app" }, "InstanceType");
const expected = "InstanceTypeMy-app";
const expected = "InstanceTypeMyapp";
expect(actual).toEqual(expected);
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/constructs/core/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export interface Identity extends StackStageIdentity, AppIdentity {}
export const AppIdentity = {
suffixText(appIdentity: AppIdentity, text: string): string {
const titleCaseApp = appIdentity.app.charAt(0).toUpperCase() + appIdentity.app.slice(1);
return `${text}${titleCaseApp}`;
// CloudFormation Logical Ids must be alphanumeric, so remove any non-alphanumeric characters: https://stackoverflow.com/a/20864946
const alphanumericTitleCaseApp = titleCaseApp.replace(/[\W_]+/g, "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely! This is a lot cleaner than my ascii code array idea!

return `${text}${alphanumericTitleCaseApp}`;
},
taggedConstruct<T extends IConstruct>(appIdentity: AppIdentity, construct: T): T {
Tags.of(construct).add("App", appIdentity.app);
Expand Down
36 changes: 18 additions & 18 deletions src/constructs/iam/roles/__snapshots__/instance-role.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,16 @@ Object {
"PolicyName": "describe-ec2-policy",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-first-app",
"Ref": "InstanceRoleMyfirstapp",
},
Object {
"Ref": "InstanceRoleMy-second-app",
"Ref": "InstanceRoleMysecondapp",
},
],
},
"Type": "AWS::IAM::Policy",
},
"GetDistributablePolicyMyfirstapp9CD90B92": Object {
"GetDistributablePolicyMyfirstappB56CBAB1": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Expand All @@ -310,16 +310,16 @@ Object {
],
"Version": "2012-10-17",
},
"PolicyName": "GetDistributablePolicyMyfirstapp9CD90B92",
"PolicyName": "GetDistributablePolicyMyfirstappB56CBAB1",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-first-app",
"Ref": "InstanceRoleMyfirstapp",
},
],
},
"Type": "AWS::IAM::Policy",
},
"GetDistributablePolicyMysecondappA8D9FE69": Object {
"GetDistributablePolicyMysecondapp5096BFDB": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Expand All @@ -346,10 +346,10 @@ Object {
],
"Version": "2012-10-17",
},
"PolicyName": "GetDistributablePolicyMysecondappA8D9FE69",
"PolicyName": "GetDistributablePolicyMysecondapp5096BFDB",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-second-app",
"Ref": "InstanceRoleMysecondapp",
},
],
},
Expand Down Expand Up @@ -391,16 +391,16 @@ Object {
"PolicyName": "GuLogShippingPolicy981BFE5A",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-first-app",
"Ref": "InstanceRoleMyfirstapp",
},
Object {
"Ref": "InstanceRoleMy-second-app",
"Ref": "InstanceRoleMysecondapp",
},
],
},
"Type": "AWS::IAM::Policy",
},
"InstanceRoleMy-first-app": Object {
"InstanceRoleMyfirstapp": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Expand Down Expand Up @@ -448,7 +448,7 @@ Object {
},
"Type": "AWS::IAM::Role",
},
"InstanceRoleMy-second-app": Object {
"InstanceRoleMysecondapp": Object {
"Properties": Object {
"AssumeRolePolicyDocument": Object {
"Statement": Array [
Expand Down Expand Up @@ -496,7 +496,7 @@ Object {
},
"Type": "AWS::IAM::Role",
},
"ParameterStoreReadMyfirstappBCF3BB3A": Object {
"ParameterStoreReadMyfirstappE5C5C329": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Expand Down Expand Up @@ -530,13 +530,13 @@ Object {
"PolicyName": "parameter-store-read-policy",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-first-app",
"Ref": "InstanceRoleMyfirstapp",
},
],
},
"Type": "AWS::IAM::Policy",
},
"ParameterStoreReadMysecondapp7B80ABE2": Object {
"ParameterStoreReadMysecondapp76C0E32C": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Expand Down Expand Up @@ -570,7 +570,7 @@ Object {
"PolicyName": "parameter-store-read-policy",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-second-app",
"Ref": "InstanceRoleMysecondapp",
},
],
},
Expand Down Expand Up @@ -606,10 +606,10 @@ Object {
"PolicyName": "ssm-run-command-policy",
"Roles": Array [
Object {
"Ref": "InstanceRoleMy-first-app",
"Ref": "InstanceRoleMyfirstapp",
},
Object {
"Ref": "InstanceRoleMy-second-app",
"Ref": "InstanceRoleMysecondapp",
},
],
},
Expand Down