in cdk/lib/cdk-stack.ts [20:143]
constructor(scope: cdk.Construct, id: string, spapi_role_arn: string, props?: cdk.StackProps) {
super(scope, id, props);
var spapiRole = spapi_role_arn;
const seller_central_app_credentials = "seller_central_app_credentials";
const codeZip = path.join(__dirname, '../../lambda/build/distributions/lambda.zip');
const ssm_seller_central_app_credentials = ssm.StringParameter.fromSecureStringParameterAttributes(this,"seller_central_app_credentials",{ parameterName:seller_central_app_credentials, version:1});
if (!spapiRole) {
spapiRole = this.initCredential();
}
// The code that defines your stack goes here
const { lambdaSG, vpc, redisCluster } = this.initInfra();
// Create EventBridge
const eventBus = new events.EventBus(this, "sp-api", { eventBusName: "sp-api" });
// const getOneNewOrderFunc = new lambda.Function(this, "GetOneNewOrder", {
// runtime: lambda.Runtime.JAVA_8,
// code: lambda.Code.fromAsset(codeZip),
// handler: 'cn.amazon.aws.rp.spapi.lambda.order.GetOneNewOrder',
// securityGroups: [lambdaSG],
// vpc,
// environment: {
// REDIS_URL: redisCluster.attrRedisEndpointAddress,
// EVENT_BUS_NAME: eventBus.eventBusName
// },
// timeout: cdk.Duration.seconds(900), // We may retry on throttling
// memorySize: 1024,
// tracing: lambda.Tracing.ACTIVE,
// retryAttempts: 0 // Retry should be controled by request limiter.
// });
// EventBridge rule to route newOrder
// const eventBusNewOrderRule = new events.Rule(this, "newOrderRule", {
// description: "Send new order to Lambda.",
// enabled: true,
// eventBus: eventBus,
// eventPattern: {
// source: ["com.aws.rapidprototyping.spapi"],
// // This filed will carry seller_id used to get seller secretes from dynamodb.
// // E.G newOrder||seller_jim
// detailType: ["{\"prefix\":\"newOrder||\"}"]
// }
// });
// dirty fix: https://github.com/aws-samples/aws-cdk-examples/issues/89#issuecomment-526758938
// const eventTargets = require("@aws-cdk/aws-events-targets");
// eventBusNewOrderRule.addTarget(new eventTargets.LambdaFunction(getOneNewOrderFunc));
// Dynamodb table
const secrtesTableName = 'spapi-secrets';
const secretsTalbe = new Table(this, 'secrtesTable', {
tableName: secrtesTableName,
partitionKey: { name: 'seller_id', type: AttributeType.STRING },
removalPolicy: cdk.RemovalPolicy.DESTROY,
// For dev/test purpose
billingMode: BillingMode.PAY_PER_REQUEST
});
const parameter = {
codeZip, lambdaSG, vpc, redisCluster, secrtesTableName, eventBus, seller_central_app_credentials, spapiRole, ssm_seller_central_app_credentials, secretsTalbe
};
const orderStack = new OrderStack(scope, parameter);
const financesStack = new FinancesStack(scope, parameter);
// Subscribe the event
const spapiEventQueue = new sqs.Queue(this, "SpApiEventQueue", {
visibilityTimeout: Duration.seconds(900)
});
// The principal is defined by SP API - it will put event to the queue.
spapiEventQueue.grantSendMessages(new iam.AccountPrincipal('437568002678'))
const logOnlyFunc = new lambda.Function(this, "LogOnly", {
runtime: lambda.Runtime.JAVA_8,
code: lambda.Code.fromAsset(codeZip),
handler: 'cn.amazon.aws.rp.spapi.lambda.log.SQSLogOnly',
securityGroups: [lambdaSG],
vpc,
timeout: cdk.Duration.seconds(100),
memorySize: 1024,
tracing: lambda.Tracing.ACTIVE,
retryAttempts: 0 // Retry should be controled by request limiter.
});
logOnlyFunc.addEventSource(new SqsEventSource(spapiEventQueue))
const eventSubscriptionFunc = new lambda.Function(this, "EventSubscription", {
runtime: lambda.Runtime.JAVA_8,
code: lambda.Code.fromAsset(codeZip),
handler: 'cn.amazon.aws.rp.spapi.lambda.notification.EventSubscription',
securityGroups: [lambdaSG],
vpc,
environment: {
REDIS_URL: redisCluster.attrRedisEndpointAddress,
SQS_ARN: spapiEventQueue.queueArn
},
timeout: cdk.Duration.seconds(900),
memorySize: 1024,
tracing: lambda.Tracing.ACTIVE,
retryAttempts: 0 // Retry should be controled by request limiter.
});
secretsTalbe.grantReadData(eventSubscriptionFunc)
// add api gateway for lambda
const api = new apigw.RestApi(this, "sp-api-subscription");
// This is used to trigger the SP-API event subscription, in a complete project this lambda should be triggered by seller register event.
const integration = new apigw.LambdaIntegration(eventSubscriptionFunc);
api.root.addMethod("GET", integration);
}