Skip to content

Commit

Permalink
fix: return valid arn from createTriggers function (#447)
Browse files Browse the repository at this point in the history
* fix: return valid arn from createTriggers function

* feat: included function to create permissions for cognito to invoke the trigger functions

* parent not set

---------

Co-authored-by: Frank <frank@sst.dev>
  • Loading branch information
jaduplessis and fwang committed May 29, 2024
1 parent 2c19c99 commit 2eab6b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/aws-cognito/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export async function handler() {}
8 changes: 7 additions & 1 deletion examples/aws-cognito/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export default $config({
};
},
async run() {
const userPool = new sst.aws.CognitoUserPool("MyUserPool");
const userPool = new sst.aws.CognitoUserPool("MyUserPool", {
triggers: {
preSignUp: {
handler: "index.handler",
},
},
});
const client = userPool.addClient("Web");
const identityPool = new sst.aws.CognitoIdentityPool("MyIdentityPool", {
userPools: [
Expand Down
36 changes: 29 additions & 7 deletions pkg/platform/src/components/aws/cognito-user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class CognitoUserPool
constructor(
name: string,
args: CognitoUserPoolArgs = {},
opts?: ComponentResourceOptions
opts?: ComponentResourceOptions,
) {
super(__pulumiType, name, args, opts);

Expand All @@ -186,14 +186,15 @@ export class CognitoUserPool
normalizeAliasesAndUsernames();
const triggers = createTriggers();
const userPool = createUserPool();
createPermissions();

this.userPool = userPool;

function normalizeAliasesAndUsernames() {
all([args.aliases, args.usernames]).apply(([aliases, usernames]) => {
if (aliases && usernames) {
throw new VisibleError(
"You cannot set both aliases and usernames. Learn more about customizing sign-in attributes at https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-aliases"
"You cannot set both aliases and usernames. Learn more about customizing sign-in attributes at https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-aliases",
);
}
});
Expand All @@ -210,11 +211,13 @@ export class CognitoUserPool
value,
{
description: `Subscribed to ${trigger} from ${name}`,
}
},
undefined,
{ parent },
);
return [trigger, fn];
})
)
return [trigger, fn.arn];
}),
),
);
}

Expand Down Expand Up @@ -276,9 +279,28 @@ export class CognitoUserPool
},
lambdaConfig: triggers,
}),
{ parent }
{ parent },
);
}

function createPermissions() {
if (!triggers) return;

triggers.apply((triggers) => {
Object.entries(triggers).forEach(([trigger, functionArn]) => {
new aws.lambda.Permission(
`${name}Permission${trigger}`,
{
action: "lambda:InvokeFunction",
function: functionArn,
principal: "cognito-idp.amazonaws.com",
sourceArn: userPool.arn,
},
{ parent },
);
});
});
}
}

/**
Expand Down

0 comments on commit 2eab6b5

Please sign in to comment.