Skip to content

Instantly share code, notes, and snippets.

@fl0wo
Last active December 12, 2022 20:12
Show Gist options
  • Save fl0wo/d781199c8c39e82bce6f38abf696d3cc to your computer and use it in GitHub Desktop.
Save fl0wo/d781199c8c39e82bce6f38abf696d3cc to your computer and use it in GitHub Desktop.
Generation of RestApi object in AWS CDK.
export const addUserApiGateway = (scope: Construct, options: InfrastructureOptions) => {
const api = new RestApi(scope, "ApiGatewayTutorial", {
deployOptions: {
cacheTtl: Duration.seconds(0),
throttlingBurstLimit: 1, // can ask JWT max 1 time!
throttlingRateLimit: 1,
},
defaultCorsPreflightOptions: {
allowMethods: ["*"],
allowOrigins: ["*"],
allowHeaders: ["*"],
allowCredentials: true,
},
});
const stytchAuthLambda = getStytchAuthorizerIntegrationLambda(scope,'StytchAuthorizer',options);
const stytchAuthorizer = new TokenAuthorizer(scope, 'CustomStytchBasicAuthAuthorizer', {
handler: stytchAuthLambda,
identitySource: 'method.request.header.Authorization',
});
const stytchLoginLambda = getStytchLoginLambdaIntegration(scope,"StytchGetLoginLambda");
const stytchRegisterLambda = getStytchRegisterLambdaIntegration(scope,"StytchGetRegisterLambda");
const stytchFromSessionIdToTokenLambda = getStytchTokenFromSessionIdLambdaIntegration(scope,"StytchGetTokenFromSessionIdLambda");
const publicEndpoints: Array<ApiRestEndpoint> = [
{
id: 'stytch_oauth_google',
fun: stytchLoginLambda,
https_methods: [HttpMethod.GET],
path: 'stytch_oauth_google',
description: "Stytch Google SSO redirects here!"
},
{
id: 'stytch_register_oauth',
fun: stytchRegisterLambda,
https_methods: [HttpMethod.GET],
path: 'stytch_register_oauth',
description: "Stytch Register SSO redirects here!"
},
{
id: 'stytch_get_token_from_session_id',
fun: stytchFromSessionIdToTokenLambda,
https_methods: [HttpMethod.GET],
path: 'stytch_token',
description: "Returns jwt stored token from session_id Stytch generated holded on SSM"
}
];
const privateEndpoints: Array<ApiRestEndpoint> = [
{
id: 'get-user-profile-endpoint',
fun: userLambda,
https_methods: [HttpMethod.GET],
path: 'user',
description: "Get user profile from if authorized!"
}
];
addEndpointsToRestApiGateway(scope,api,publicEndpoints);
addPrivateEndpointsToRestApiGateway(scope,api,
stytchAuthorizer,
AuthorizationType.CUSTOM, // Custom: we'll use Stytch not Cognito
privateEndpoints
);
return api;
}
@fl0wo
Copy link
Author

fl0wo commented Dec 12, 2022

The methods to add the array of ApiRestEndpoint objects to the RestApi is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment