forked from joeduffy/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
297 lines (260 loc) · 7.44 KB
/
index.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Athena
const databaseBucket = new aws.s3.Bucket("mydatabasebucket");
const database = new aws.athena.Database("mydatabase", {
name: "mydatabase",
bucket: databaseBucket.bucket
});
const namedQuery = new aws.athena.NamedQuery("mynamedquery", {
database: database.id,
query: database.id.apply(n => `SELECT * FROM ${n} limit 10;`)
});
// CloudWatch
const dashboard = new aws.cloudwatch.Dashboard("mydashboard", {
dashboardName: "my-dashboard",
dashboardBody: JSON.stringify({
widgets: [
{
type: "metric",
x: 0,
y: 0,
width: 12,
height: 6,
properties: {
metrics: [
[
"AWS/EC2",
"CPUUtilization",
"InstanceId",
"i-012345"
]
],
period: 300,
stat: "Average",
region: "us-east-1",
title: "EC2 Instance CPU"
}
},
{
type: "text",
x: 0,
y: 7,
width: 3,
height: 3,
properties: {
markdown: "Hello world"
}
}
]
})
});
const loginsTopic = new aws.sns.Topic("myloginstopic");
const eventRule = new aws.cloudwatch.EventRule("myeventrule", {
eventPattern: JSON.stringify({
"detail-type": [
"AWS Console Sign In via CloudTrail"
]
})
});
const eventTarget = new aws.cloudwatch.EventTarget("myeventtarget", {
rule: eventRule.name,
targetId: "SendToSNS",
arn: loginsTopic.arn
})
const logGroup = new aws.cloudwatch.LogGroup("myloggroup");
const logMetricFilter = new aws.cloudwatch.LogMetricFilter("mylogmetricfilter", {
pattern: "",
logGroupName: logGroup.name,
metricTransformation: {
name: "EventCount",
namespace: "YourNamespace",
value: "1"
}
});
const logStream = new aws.cloudwatch.LogStream("mylogstream", {
logGroupName: logGroup.name
});
const metricAlarm = new aws.cloudwatch.MetricAlarm("mymetricalarm", {
comparisonOperator: "GreaterThanOrEqualToThreshold",
evaluationPeriods: 2,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 120,
statistic: "Average",
threshold: 80,
alarmDescription: "This metric monitors ec2 cpu utilization"
});
// DynamoDB
const db = new aws.dynamodb.Table("mytable", {
attributes: [
{ name: "Id", type: "S" },
],
hashKey: "Id",
readCapacity: 1,
writeCapacity: 1,
});
// EC2
const eip = new aws.ec2.Eip("myeip");
const securityGroup = new aws.ec2.SecurityGroup("mysecuritygroup", {
ingress: [
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
});
const vpc = new aws.ec2.Vpc("myvpc", {
cidrBlock: "10.0.0.0/16"
})
const internetGateway = new aws.ec2.InternetGateway("myinternetgateway", {
vpcId: vpc.id,
});
const publicRouteTable = new aws.ec2.RouteTable("myroutetable", {
routes: [
{
cidrBlock: "0.0.0.0/0",
gatewayId: internetGateway.id,
},
],
vpcId: vpc.id,
});
// ECR
const repository = new aws.ecr.Repository("myrepository");
const repositoryPolicy = new aws.ecr.RepositoryPolicy("myrepositorypolicy", {
repository: repository.id,
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Sid: "new policy",
Effect: "Allow",
Principal: "*",
Action: [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:DescribeRepositories",
"ecr:GetRepositoryPolicy",
"ecr:ListImages",
"ecr:DeleteRepository",
"ecr:BatchDeleteImage",
"ecr:SetRepositoryPolicy",
"ecr:DeleteRepositoryPolicy"
]
}]
})
});
const lifecyclePolicy = new aws.ecr.LifecyclePolicy("mylifecyclepolicy", {
repository: repository.id,
policy: JSON.stringify({
rules: [{
rulePriority: 1,
description: "Expire images older than 14 days",
selection: {
tagStatus: "untagged",
countType: "sinceImagePushed",
countUnit: "days",
countNumber: 14
},
action: {
type: "expire"
}
}]
})
});
// ECS
const cluster = new aws.ecs.Cluster("mycluster");
// IAM
const role = new aws.iam.Role("myrole", {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: "sts:AssumeRole",
Principal: {
Service: "ec2.amazonaws.com"
},
Effect: "Allow",
Sid: ""
}]
})
});
const rolePolicy = new aws.iam.RolePolicy("myrolepolicy", {
role: role.id,
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: [ "ec2:Describe*" ],
Effect: "Allow",
Resource: "*"
}]
})
});
const policy = new aws.iam.Policy("mypolicy", {
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: [
"ec2:Describe*"
],
Effect: "Allow",
Resource: "*"
}]
})
});
const rolePolicyAttachment = new aws.iam.RolePolicyAttachment("myrolepolicyattachment", {
role: role,
policyArn: policy.arn
});
const user = new aws.iam.User("myuser");
const group = new aws.iam.Group("mygroup");
const policyAttachment = new aws.iam.PolicyAttachment("mypolicyattachment", {
users: [user],
groups: [group],
roles: [role],
policyArn: policy.arn
});
// Kinesis
const stream = new aws.kinesis.Stream("mystream", {
shardCount: 1
});
// S3
const bucket = new aws.s3.Bucket("my-bucket");
const bucketMetric = new aws.s3.BucketMetric("my-bucket-metric", {
bucket: bucket.bucket
});
const bucketNotification = new aws.s3.BucketNotification("my-bucket-notification", {
bucket: bucket.bucket
});
const bucketObject = new aws.s3.BucketObject("my-bucket-object", {
bucket: bucket.bucket,
content: "hello world"
});
const bucketPolicy = new aws.s3.BucketPolicy("my-bucket-policy", {
bucket: bucket.bucket,
policy: bucket.bucket.apply(publicReadPolicyForBucket)
})
function publicReadPolicyForBucket(bucketName: string) {
return JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: "*",
Action: [
"s3:GetObject"
],
Resource: [
`arn:aws:s3:::${bucketName}/*` // policy refers to bucket name explicitly
]
}]
});
}
// SQS
const queue = new aws.sqs.Queue("myqueue");
// SNS
const topic = new aws.sns.Topic("mytopic");
const topicSubscription = new aws.sns.TopicSubscription("mytopicsubscription", {
topic: topic,
protocol: "sqs",
endpoint: queue.arn
});