Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public get eventRuleTarget(): EventRuleTarget {
if (!this.eventRuleTargetPolicyAdded) {
this.addToResourcePolicy(new PolicyStatement()
.addAction('sns:Publish')
.addPrincipal(new ServicePrincipal('events.amazonaws.com'))
.addResource(this.topicArn));
this.eventRuleTargetPolicyAdded = true;
}
return {
id: this.name,
arn: this.topicArn,
};
}
}
public subscribeLambda(lambdaFunction: LambdaRef) {
const subscriptionName = lambdaFunction.name + 'Subscription';
if (this.tryFindChild(subscriptionName)) {
throw new Error(`A subscription between the topic ${this.name} and the lambda ${lambdaFunction.name} already exists`);
}
const sub = new Subscription(this, subscriptionName, {
topic: this,
endpoint: lambdaFunction.functionArn,
protocol: SubscriptionProtocol.Lambda
});
lambdaFunction.addPermission(this.name, {
sourceArn: this.topicArn,
principal: new ServicePrincipal('sns.amazonaws.com'),
});
return sub;
}