-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
167 lines (131 loc) · 4.17 KB
/
main.tf
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
data "aws_iam_role" "ArnRole" {
name = "$IAM1-role name" # add your aws IAM role name here.
}
## retrive exsiting aws subnet id using data function.
data "aws_subnet" "selected" {
id = var.subnet_id
}
## retrive exsiting aws SG id using data function.
data "aws_security_group" "sg1" {
id = var.security_group_id
}
# creating AWS image builder pipeline.
resource "aws_imagebuilder_image_pipeline" "AMIexample" {
image_recipe_arn = aws_imagebuilder_image_recipe.imager_recipe.arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.infrastructure_configuration.arn
name = "AmiPipeline"
status = "ENABLED"
description = "This is a test AmiPipeline "
# you can remove this block if you are planning to trigger pipeline mannually.
schedule {
schedule_expression = "cron(0 8 ? * tue)"
pipeline_execution_start_condition = "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
}
image_tests_configuration {
image_tests_enabled = true
timeout_minutes = 120
}
tags = {
"Name" = "AmiPipeline"
}
}
# This is a inline imagebuilder component.
resource "aws_imagebuilder_component" "AMIexample" {
data = yamlencode({
phases = [{
name = "build"
steps = [{
action = "ExecuteBash"
inputs = {
commands = ["yum install httpd -y"]
}
name = "Loginexample"
onFailure = "Continue"
}]
}]
schemaVersion = 1.0
})
name = "example"
platform = "Linux"
version = "1.0.0"
}
#### create builder component using YAML file.
resource "aws_s3_bucket_object" "AMIYamL" {
bucket = var.aws_s3_bucket_object
key = "SmsLogin-Custom.yml"
source = "SmsLogin-Custom.yml"
etag = filemd5("SSM-Custom.yml")
}
resource "aws_imagebuilder_component" "SSMComponent" {
name = "AMILogin-Demo"
platform = "Linux"
uri = "s3://${var.aws_s3_bucket_object}/SSM-Custom.yml"
version = "1.0.0"
depends_on = [
aws_s3_bucket_object.AMIYamL
]
}
resource "aws_imagebuilder_image" "AMIBuilder" {
distribution_configuration_arn = aws_imagebuilder_distribution_configuration.AMIexample.arn
image_recipe_arn = aws_imagebuilder_image_recipe.imager_recipe.arn
infrastructure_configuration_arn = aws_imagebuilder_infrastructure_configuration.infrastructure_configuration.arn
}
resource "aws_imagebuilder_image_recipe" "imager_recipe" {
block_device_mapping {
device_name = "/dev/xvdb"
ebs {
delete_on_termination = true
volume_size = var.ebsvol_size
volume_type = var.ebs_type
}
}
component {
component_arn = aws_imagebuilder_component.SSMComponent.arn
}
name = var.name_recipe
parent_image = var.image_id
version = var.image_version
}
resource "aws_s3_bucket" "AMILogs" {
bucket = "$yourS3bucketname"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
resource "aws_imagebuilder_infrastructure_configuration" "infrastructure_configuration" {
description = "infastructure component build"
instance_profile_name = "login-imagebuilder-tf"
instance_types = ["t2.nano"] // need to set var
key_pair = var.keypair_name
name = "AMIDemo"
security_group_ids = [data.aws_security_group.sg1.id]
#sns_topic_arn = aws_sns_topic.example.arn # enable this if you're planning to enable SNS.
subnet_id = data.aws_subnet.selected.id
terminate_instance_on_failure = true
logging {
s3_logs {
s3_bucket_name = "$your-loggingbucket-name" #s3 bucket name for logging.
s3_key_prefix = "Imagebuilder-Logs" #s3 prefix to stroe logs.
}
}
depends_on = [
aws_s3_bucket.AMILogs
]
tags = {
Name = "Infra"
}
}
resource "aws_imagebuilder_distribution_configuration" "AMIexample" {
name = "SmsLogin"
distribution {
ami_distribution_configuration {
ami_tags = {
Product = "AMI"
Environment = "Dev"
}
name = "AMI-linux-{{ imagebuilder:buildDate }}"
}
region = "us-east-1"
}
}