Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Use handlebars to update IAM role placeholders instead of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed Oct 30, 2023
1 parent b6f1f8e commit d3b9615
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
45 changes: 44 additions & 1 deletion iam/package-lock.json

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

3 changes: 2 additions & 1 deletion iam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"dependencies": {
"@pulumi/aws": "^6.6.1",
"@pulumi/pulumi": "^3.91.0"
"@pulumi/pulumi": "^3.91.0",
"handlebars": "^4.7.8"
}
}
17 changes: 8 additions & 9 deletions iam/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { iam, getCallerIdentity } from "@pulumi/aws";
import { getCluster } from "@pulumi/aws/eks";
import { readFileSync } from "fs";
import { compile } from "handlebars";

export function createPolicy(file: string, policyName: string) {
const policy = readFileSync(file, "utf8");
Expand All @@ -16,16 +17,14 @@ export function createRole(
clusterId: string,
) {
const placeholderRole = readFileSync(file, "utf8");
const template = compile(placeholderRole)

const updatedPlaceholders = {
AWS_ACCOUNT_ID: accountId,
EKS_CLUSTER_ID: clusterId,
}

const partiallyUpdatedRole = placeholderRole.replace(
new RegExp("{{AWS_ACCOUNT_ID}}", "g"),
accountId,
);

const updatedRole = partiallyUpdatedRole.replace(
new RegExp("{{EKS_CLUSTER_ID}}", "g"),
clusterId,
);
const updatedRole = template(updatedPlaceholders)

return new iam.Role(roleName, {
assumeRolePolicy: updatedRole,
Expand Down

0 comments on commit d3b9615

Please sign in to comment.