Skip to content

Commit 269b61a

Browse files
committed
fix: adjust iam
1 parent c446c32 commit 269b61a

File tree

2 files changed

+102
-35
lines changed

2 files changed

+102
-35
lines changed

src/components/AsyncTextExtract.ts

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,29 @@ export class AsyncTextExtract extends pulumi.ComponentResource {
6262

6363
this.bucket = bucket
6464

65+
const topicName = `${name}-sns-topic`
66+
this.snsTopic = new aws.sns.Topic(
67+
topicName,
68+
{
69+
name: topicName
70+
},
71+
defaultResourceOptions
72+
)
73+
6574
// 'AmazonTextract' will be used to create inline policy for iam:PassRole.
6675
// refer to https://docs.aws.amazon.com/textract/latest/dg/api-async-roles.html#api-async-roles-all-topics (step 8)
67-
const roleName = `AmazonTextract${name}-role`
76+
const roleName = `${name}-textract-service-role`
6877
this.role = new aws.iam.Role(
6978
roleName,
7079
{
7180
name: roleName,
7281
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
73-
Service: ['ec2.amazonaws.com', 'textract.amazonaws.com', 'lambda.amazonaws.com']
82+
Service: ['textract.amazonaws.com', 'lambda.amazonaws.com']
7483
})
7584
},
7685
defaultResourceOptions
7786
)
7887

79-
const topicName = `${name}-sns-topic`
80-
this.snsTopic = new aws.sns.Topic(
81-
topicName,
82-
{
83-
name: topicName
84-
},
85-
defaultResourceOptions
86-
)
8788
this.snsPolicy = new SNSPublishPolicy(`${topicName}-policy`, { topicArn: this.snsTopic.arn })
8889
new aws.iam.RolePolicyAttachment(
8990
`${topicName}-policy-attachment`,
@@ -101,30 +102,31 @@ export class AsyncTextExtract extends pulumi.ComponentResource {
101102
if (!RoleArn || !SNSTopicArn) {
102103
throw new Error('Required ENV are not present')
103104
}
104-
const extract = new AWS.Textract({})
105-
try {
106-
for (const record of records) {
107-
extract
108-
.startDocumentTextDetection({
109-
JobTag: record.s3.object.key,
110-
DocumentLocation: {
111-
S3Object: {
112-
Bucket: record.s3.bucket.name,
113-
Name: record.s3.object.key
114-
}
115-
},
116-
NotificationChannel: {
117-
RoleArn,
118-
SNSTopicArn
119-
}
120-
})
121-
.send()
122-
}
123-
124-
callback(undefined, undefined)
125-
} catch (error) {
126-
callback(error, undefined)
127-
}
105+
const extract = new AWS.Textract({ logger: console, region: aws.config.region })
106+
const [record] = records
107+
const Bucket = record.s3.bucket.name
108+
const key = record.s3.object.key
109+
110+
return extract
111+
.startDocumentTextDetection({
112+
JobTag: record.s3.object.key,
113+
DocumentLocation: {
114+
S3Object: {
115+
Bucket,
116+
Name: key
117+
}
118+
},
119+
NotificationChannel: {
120+
RoleArn,
121+
SNSTopicArn
122+
}
123+
})
124+
.promise()
125+
.then(data => {
126+
console.log(data)
127+
callback(undefined, undefined)
128+
})
129+
.catch(err => callback(err, undefined))
128130
}
129131

130132
const lambdaName = `${name}-lambda-callback`
@@ -145,7 +147,37 @@ export class AsyncTextExtract extends pulumi.ComponentResource {
145147
defaultResourceOptions
146148
)
147149

148-
new LambdaCloudWatchPolicy(`${name}-policy`, { lambdaName }, defaultResourceOptions)
150+
const cloudwatchPolicy = new LambdaCloudWatchPolicy(
151+
`${name}-cloudwatch-policy`,
152+
{ lambdaName },
153+
defaultResourceOptions
154+
)
155+
new aws.iam.RolePolicyAttachment(
156+
`${name}-cloudwatch-policy-attachment`,
157+
{
158+
policyArn: cloudwatchPolicy.policy.arn,
159+
role: roleName
160+
},
161+
defaultResourceOptions
162+
)
163+
164+
new aws.iam.RolePolicy(
165+
name,
166+
{
167+
role: this.role,
168+
policy: {
169+
Version: '2012-10-17',
170+
Statement: [
171+
{
172+
Effect: 'Allow',
173+
Action: ['textract:*'],
174+
Resource: this.callbackFunction.arn
175+
}
176+
]
177+
}
178+
},
179+
defaultResourceOptions
180+
)
149181

150182
this.bucketEventSubscription = bucket.onObjectCreated(
151183
`${name}-AsyncTextExtractor-onUpload`,

src/components/policies/s3.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as aws from '@pulumi/aws'
2+
import * as pulumi from '@pulumi/pulumi'
3+
4+
interface S3ReadPolicyArgs {
5+
name: string
6+
bucketArn: pulumi.Output<aws.ARN>
7+
}
8+
9+
export class S3ReadPolicy extends pulumi.ComponentResource {
10+
readonly policy: aws.iam.Policy
11+
12+
constructor(args: S3ReadPolicyArgs, opts?: pulumi.ComponentResourceOptions) {
13+
super('aws:components:S3ReadPolicy', args.name, args, opts)
14+
const { name, bucketArn } = args
15+
const defaultParentOptions: pulumi.ResourceOptions = { parent: this }
16+
const policy = new aws.iam.Policy(
17+
name,
18+
{
19+
name,
20+
policy: {
21+
Version: '2012-10-17',
22+
Statement: [
23+
{
24+
Action: ['s3:GetObject'],
25+
Effect: 'Allow',
26+
Resource: pulumi.interpolate`${bucketArn}/*`
27+
}
28+
]
29+
}
30+
},
31+
defaultParentOptions
32+
)
33+
this.policy = policy
34+
}
35+
}

0 commit comments

Comments
 (0)