-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdk-ec2-image-builder-cardano-node.go
234 lines (204 loc) · 8.94 KB
/
cdk-ec2-image-builder-cardano-node.go
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
package main
import (
"log"
"os"
"github.com/aws/aws-cdk-go/awscdk/v2"
// "github.com/aws/aws-cdk-go/awscdk/v2/awssqs"
"github.com/aws/aws-cdk-go/awscdk/v2/awscloudtrail"
"github.com/aws/aws-cdk-go/awscdk/v2/awsiam"
"github.com/aws/aws-cdk-go/awscdk/v2/awsimagebuilder"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
"github.com/aws/aws-cdk-go/awscdk/v2/awslogs"
"github.com/aws/aws-cdk-go/awscdk/v2/awslogsdestinations"
"github.com/aws/aws-cdk-go/awscdk/v2/awss3"
"github.com/aws/aws-cdk-go/awscdk/v2/awss3assets"
"github.com/aws/aws-cdk-go/awscdk/v2/awssns"
"github.com/aws/aws-cdk-go/awscdk/v2/awssnssubscriptions"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
)
type CdkEc2ImageBuilderCardanoNodeStackProps struct {
awscdk.StackProps
}
func NewCdkEc2ImageBuilderCardanoNodeStack(scope constructs.Construct, id string, props *CdkEc2ImageBuilderCardanoNodeStackProps) awscdk.Stack {
var sprops awscdk.StackProps
if props != nil {
sprops = props.StackProps
}
stack := awscdk.NewStack(scope, &id, &sprops)
content, err := os.ReadFile("component.yaml")
if err != nil {
log.Fatal(err)
}
// The code that defines your stack goes here
component := awsimagebuilder.NewCfnComponent(stack, jsii.String("Component"), &awsimagebuilder.CfnComponentProps{
Data: jsii.String(string(content)),
Name: jsii.String("whslabs-cardano-node"),
Platform: jsii.String("Linux"),
Version: jsii.String("1.0.0"),
})
imageRecipe := awsimagebuilder.NewCfnImageRecipe(stack, jsii.String("ImageRecipe"), &awsimagebuilder.CfnImageRecipeProps{
Name: jsii.String("whslabs-cardano-node"),
ParentImage: awscdk.Fn_Sub(jsii.String("arn:${AWS::Partition}:imagebuilder:${AWS::Region}:aws:image/amazon-linux-2-x86/x.x.x"), nil),
Version: jsii.String("1.0.0"),
BlockDeviceMappings: []interface{}{
&awsimagebuilder.CfnImageRecipe_InstanceBlockDeviceMappingProperty{
DeviceName: jsii.String("/dev/xvda"),
Ebs: &awsimagebuilder.CfnImageRecipe_EbsInstanceBlockDeviceSpecificationProperty{
DeleteOnTermination: jsii.Bool(true),
VolumeSize: jsii.Number(20),
},
},
},
Components: []interface{}{
&awsimagebuilder.CfnImageRecipe_ComponentConfigurationProperty{
ComponentArn: component.AttrArn(),
},
},
})
bucket := awss3.NewBucket(stack, jsii.String("BucketImageBuilder"), &awss3.BucketProps{
BlockPublicAccess: awss3.BlockPublicAccess_BLOCK_ALL(),
RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
})
roleImageBuilder := awsiam.NewRole(stack, jsii.String("RoleImageBuilder"), &awsiam.RoleProps{
AssumedBy: awsiam.NewServicePrincipal(jsii.String("ec2.amazonaws.com"), nil),
ManagedPolicies: &[]awsiam.IManagedPolicy{
awsiam.ManagedPolicy_FromAwsManagedPolicyName(jsii.String("AmazonSSMManagedInstanceCore")),
awsiam.ManagedPolicy_FromAwsManagedPolicyName(jsii.String("EC2InstanceProfileForImageBuilder")),
},
})
roleImageBuilder.AttachInlinePolicy(awsiam.NewPolicy(stack, jsii.String("PolicyImageBuilder"), &awsiam.PolicyProps{
Statements: &[]awsiam.PolicyStatement{
awsiam.NewPolicyStatement(&awsiam.PolicyStatementProps{
Actions: &[]*string{
jsii.String("s3:PutObject"),
},
Resources: &[]*string{
bucket.ArnForObjects(jsii.String("*")),
},
}),
},
}))
instanceProfile := awsiam.NewCfnInstanceProfile(stack, jsii.String("InstanceProfile"), &awsiam.CfnInstanceProfileProps{
Roles: &[]*string{
roleImageBuilder.RoleName(),
},
})
infrastructureConfiguration := awsimagebuilder.NewCfnInfrastructureConfiguration(stack, jsii.String("InfrastructureConfiguration"), &awsimagebuilder.CfnInfrastructureConfigurationProps{
InstanceProfileName: instanceProfile.Ref(),
Name: jsii.String("whslabs-cardano-node"),
Logging: &awsimagebuilder.CfnInfrastructureConfiguration_LoggingProperty{
S3Logs: &awsimagebuilder.CfnInfrastructureConfiguration_S3LogsProperty{
S3BucketName: bucket.BucketName(),
},
},
})
awsimagebuilder.NewCfnImagePipeline(stack, jsii.String("ImagePipline"), &awsimagebuilder.CfnImagePipelineProps{
ImageRecipeArn: imageRecipe.AttrArn(),
InfrastructureConfigurationArn: infrastructureConfiguration.AttrArn(),
Name: jsii.String("whslabs-cardano-node"),
Status: jsii.String("DISABLED"),
})
cloudtrail := awscloudtrail.NewTrail(stack, jsii.String("CloudTrail"), &awscloudtrail.TrailProps{
Bucket: awss3.NewBucket(stack, jsii.String("BucketCloudTrail"), &awss3.BucketProps{
BlockPublicAccess: awss3.BlockPublicAccess_BLOCK_ALL(),
RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
}),
SendToCloudWatchLogs: jsii.Bool(true),
})
topic := awssns.NewTopic(stack, jsii.String("Topic"), nil)
topic.AddSubscription(awssnssubscriptions.NewEmailSubscription(jsii.String("[email protected]"), nil))
roleFunction := awsiam.NewRole(stack, jsii.String("RoleFunction"), &awsiam.RoleProps{
AssumedBy: awsiam.NewServicePrincipal(jsii.String("lambda.amazonaws.com"), nil),
})
roleFunction.AttachInlinePolicy(awsiam.NewPolicy(stack, jsii.String("PolicyFunction"), &awsiam.PolicyProps{
Statements: &[]awsiam.PolicyStatement{
awsiam.NewPolicyStatement(&awsiam.PolicyStatementProps{
Actions: &[]*string{
jsii.String("sns:Publish"),
},
Resources: &[]*string{
topic.TopicArn(),
},
}),
awsiam.NewPolicyStatement(&awsiam.PolicyStatementProps{
Actions: &[]*string{
jsii.String("logs:CreateLogGroup"),
jsii.String("logs:CreateLogStream"),
jsii.String("logs:PutLogEvents"),
},
Resources: &[]*string{
jsii.String("*"),
},
}),
},
}))
lambda := awslambda.NewFunction(stack, jsii.String("Function"), &awslambda.FunctionProps{
Code: awslambda.Code_FromAsset(jsii.String("./"), &awss3assets.AssetOptions{
Bundling: &awscdk.BundlingOptions{
Image: awscdk.DockerImage_FromBuild(jsii.String("./"), &awscdk.DockerBuildOptions{
File: jsii.String("Dockerfile.lambda"),
}),
Command: &[]*string{jsii.String(`nix build && cp result-bin/bin/rust-lambda-cloudtrail /asset-output/bootstrap`)},
Entrypoint: &[]*string{jsii.String("/bin/sh"), jsii.String("-c")},
User: jsii.String("root:root"),
},
}),
Handler: jsii.String("dummy"),
Runtime: awslambda.Runtime_PROVIDED_AL2(),
Environment: &map[string]*string{
"CLOUDWATCH_BASE_URL": jsii.String("https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logsV2:log-groups/log-group/$252Faws$252Fimagebuilder$252F"),
"TOPIC_ARN": topic.TopicArn(),
},
Role: roleFunction,
})
lambda.AddPermission(jsii.String("Permission"), &awslambda.Permission{
Principal: awsiam.NewServicePrincipal(jsii.String("logs.amazonaws.com"), nil),
Action: jsii.String("lambda:InvokeFunction"),
SourceArn: cloudtrail.LogGroup().LogGroupArn(),
})
awslogs.NewSubscriptionFilter(stack, jsii.String("SubscriptionFilter"), &awslogs.SubscriptionFilterProps{
Destination: awslogsdestinations.NewLambdaDestination(lambda, nil),
FilterPattern: awslogs.FilterPattern_All(awslogs.FilterPattern_StringValue(jsii.String("$.userAgent"), jsii.String("="), jsii.String("imagebuilder.amazonaws.com")), awslogs.FilterPattern_StringValue(jsii.String("$.eventName"), jsii.String("="), jsii.String("CreateImage"))),
LogGroup: cloudtrail.LogGroup(),
})
// example resource
// queue := awssqs.NewQueue(stack, jsii.String("CdkEc2ImageBuilderCardanoNodeQueue"), &awssqs.QueueProps{
// VisibilityTimeout: awscdk.Duration_Seconds(jsii.Number(300)),
// })
return stack
}
func main() {
defer jsii.Close()
app := awscdk.NewApp(nil)
NewCdkEc2ImageBuilderCardanoNodeStack(app, "CdkEc2ImageBuilderCardanoNodeStack", &CdkEc2ImageBuilderCardanoNodeStackProps{
awscdk.StackProps{
Env: env(),
},
})
app.Synth(nil)
}
// env determines the AWS environment (account+region) in which our stack is to
// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html
func env() *awscdk.Environment {
// If unspecified, this stack will be "environment-agnostic".
// Account/Region-dependent features and context lookups will not work, but a
// single synthesized template can be deployed anywhere.
//---------------------------------------------------------------------------
return nil
// Uncomment if you know exactly what account and region you want to deploy
// the stack to. This is the recommendation for production stacks.
//---------------------------------------------------------------------------
// return &awscdk.Environment{
// Account: jsii.String("123456789012"),
// Region: jsii.String("us-east-1"),
// }
// Uncomment to specialize this stack for the AWS Account and Region that are
// implied by the current CLI configuration. This is recommended for dev
// stacks.
//---------------------------------------------------------------------------
// return &awscdk.Environment{
// Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
// Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
// }
}