-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomRes
More file actions
61 lines (46 loc) · 2.62 KB
/
customRes
File metadata and controls
61 lines (46 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.myaws;
import java.util.List;
import java.util.Map;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.customresources.AwsCustomResource;
import software.amazon.awscdk.customresources.AwsCustomResourcePolicy;
import software.amazon.awscdk.customresources.AwsSdkCall;
import software.amazon.awscdk.customresources.PhysicalResourceId;
import software.amazon.awscdk.services.dynamodb.Attribute;
import software.amazon.awscdk.services.dynamodb.AttributeType;
import software.amazon.awscdk.services.dynamodb.BillingMode;
import software.amazon.awscdk.services.dynamodb.Table;
import software.amazon.awscdk.services.iam.Effect;
import software.amazon.awscdk.services.iam.PolicyStatement;
import software.constructs.Construct;
public class AlexaSkillStack extends Stack {
public AlexaSkillStack(final Construct scope, final String id) {
this(scope, id, null);
}
public AlexaSkillStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Table table = Table.Builder.create(this, "sampleTable").tableName("testBasicTable")
.billingMode(BillingMode.PAY_PER_REQUEST)
.partitionKey(Attribute.builder().name("ID").type(AttributeType.NUMBER).build()).build();
createCustomRes(table);
createIntialData(table);
}
private void createCustomRes(Table table) {
AwsCustomResource tableInitializationResource = AwsCustomResource.Builder.create(this, "tableInitilizationCustomResouce")
.policy(AwsCustomResourcePolicy.fromStatements(List.of(PolicyStatement.Builder.create().effect(Effect.ALLOW)
.actions(List.of("dynamodb:PutItem"))
.resources(List.of(table.getTableArn())).build()
))).onCreate(createIntialData(table)).build();
tableInitializationResource.getNode().addDependency(table);
}
private AwsSdkCall createIntialData(Table table) {
return AwsSdkCall.builder().service("DynamoDB").action("putItem")
.physicalResourceId(PhysicalResourceId.of(table.getTableName() + "_initializer"))
.parameters(Map.ofEntries(Map.entry("TableName", table.getTableName()),
Map.entry("Item",
Map.ofEntries(Map.entry("ID", Map.of("N","1")), Map.entry("tip", Map.of("S", "hello from customResource")))),
Map.entry("ConditionExpression", "attribute_not_exists(ID)")))
.build();
}
}