Skip to content

Commit a434f72

Browse files
committed
fix(schedules): Add policy to sns topics to allow eventbridge trigger
1 parent a0f1b7e commit a434f72

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/plugins/aws/src/resources/schedule.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
import { cronToAwsCron } from './schedule';
215

316
describe('Cron Expression Conversion', () => {
@@ -78,7 +91,7 @@ describe('Cron Expression Conversion', () => {
7891
describe('When converting the expression', () => {
7992
let awsExpValues: string[] = [];
8093
beforeAll(() => {
81-
// Expected result = '0/1 * ? * 1 *'
94+
// Expected result = '0/1 * ? * 2-4 *'
8295
awsExpValues = cronToAwsCron(exp).split(' ');
8396
});
8497

packages/plugins/aws/src/resources/schedule.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,35 @@ export class NitricScheduleEventBridge extends pulumi.ComponentResource {
137137
},
138138
defaultResourceOptions,
139139
);
140+
141+
const snsTopicSchedulePolicy = topic.sns.arn.apply((arn) =>
142+
aws.iam.getPolicyDocument({
143+
// TODO: According to the docs, 'conditions' are not supported for a policy involving EventBridge
144+
// See: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-use-resource-based.html#eb-sns-permissions
145+
// "You can't use of Condition blocks in Amazon SNS topic policies for EventBridge."
146+
// This means any EventBridge rule will be able to publish to this topic.
147+
policyId: '__default_policy_ID',
148+
statements: [
149+
{
150+
sid: '__default_statement_ID',
151+
effect: 'Allow',
152+
actions: ['SNS:Publish'],
153+
principals: [
154+
{
155+
type: 'Service',
156+
identifiers: ['events.amazonaws.com'],
157+
},
158+
],
159+
resources: [arn],
160+
},
161+
],
162+
}),
163+
);
164+
165+
new aws.sns.TopicPolicy(`${schedule.name}Target${topic.name}Policy`, {
166+
arn: topic.sns.arn,
167+
policy: snsTopicSchedulePolicy.apply((snsTopicPolicy) => snsTopicPolicy.json),
168+
});
140169
}
141170

142171
this.registerOutputs({

0 commit comments

Comments
 (0)