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

Commit

Permalink
component: iamEdit helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Oct 12, 2024
1 parent 58ff05e commit 87126d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions examples/aws-bucket-policy/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ export default $config({
const bucket = new sst.aws.Bucket("MyBucket", {
transform: {
policy: (args) => {
// use $jsonParse and $jsonStringify helper functions to manipulate JSON strings
// use sst.aws.iamEdit helper function to manipulate IAM policy
// containing Output values from components
args.policy = $jsonParse(args.policy).apply((policy) => {
args.policy = sst.aws.iamEdit(args.policy, (policy) => {
policy.Statement.push({
Effect: "Allow",
Principal: { Service: "ses.amazonaws.com" },
Action: "s3:PutObject",
Resource: $interpolate`arn:aws:s3:::${args.bucket}/*`,
});
return $jsonStringify(policy);
});
},
},
Expand Down
22 changes: 22 additions & 0 deletions platform/src/components/aws/iam-edit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Input, jsonStringify, output, UnwrappedObject } from "@pulumi/pulumi";
import { iam } from "@pulumi/aws";
import { Prettify } from "../component";

type PartialUnwrappedPolicyDocument = {
Id?: string;
Version: "2008-10-17" | "2012-10-17";
Statement: Input<iam.PolicyStatement>[];
};

export function iamEdit(
policy: Input<iam.PolicyDocument | string>,
cb: (doc: Prettify<PartialUnwrappedPolicyDocument>) => void,
) {
return output(policy).apply((v) => {
const json = typeof v === "string" ? JSON.parse(v) : v;
cb(json);
return iam.getPolicyDocumentOutput({
sourcePolicyDocuments: [jsonStringify(json)],
}).json;
});
}
1 change: 1 addition & 0 deletions platform/src/components/aws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export * from "./vpc.js";
export * from "./react.js";
export { linkable } from "./linkable.js";
export { permission } from "./permission.js";
export { iamEdit } from "./iam-edit.js";

// internal components
export * from "./cdn.js";
Expand Down

0 comments on commit 87126d5

Please sign in to comment.