-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
130 lines (117 loc) · 2.78 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-west-2"
}
variable "iplz_img_path" {
description = "Path to the server image."
type = string
}
output "public_ip" {
value = aws_instance.iplz_server.public_ip
}
resource "aws_instance" "iplz_server" {
ami = aws_ami.iplz_ami.id
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.iplz_security_group.id]
}
resource "aws_security_group" "iplz_security_group" {
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_ami" "iplz_ami" {
name = "iplz_server_ami"
virtualization_type = "hvm"
root_device_name = "/dev/xvda"
ebs_block_device {
device_name = "/dev/xvda"
snapshot_id = aws_ebs_snapshot_import.iplz_import.id
}
}
resource "aws_s3_bucket" "iplz_bucket" {}
resource "aws_s3_bucket_acl" "iplz_acl" {
bucket = aws_s3_bucket.iplz_bucket.id
acl = "private"
}
resource "aws_s3_object" "image_upload" {
bucket = aws_s3_bucket.iplz_bucket.id
key = "iplz.vhd"
source = var.iplz_img_path
}
resource "aws_ebs_snapshot_import" "iplz_import" {
role_name = aws_iam_role.vmimport_role.id
disk_container {
format = "VHD"
user_bucket {
s3_bucket = aws_s3_bucket.iplz_bucket.id
s3_key = aws_s3_object.image_upload.id
}
}
lifecycle {
replace_triggered_by = [
aws_s3_object.image_upload
]
}
}
resource "aws_iam_role_policy_attachment" "vmpimport_attach" {
role = aws_iam_role.vmimport_role.id
policy_arn = aws_iam_policy.vmimport_policy.arn
}
resource "aws_iam_role" "vmimport_role" {
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = { Service = "vmie.amazonaws.com" }
Action = "sts:AssumeRole"
Condition = {
StringEquals = {
"sts:Externalid" = "vmimport"
}
}
}
]
})
}
resource "aws_iam_policy" "vmimport_policy" {
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:GetBucketAcl"
]
Resource = [
"arn:aws:s3:::${aws_s3_bucket.iplz_bucket.id}",
"arn:aws:s3:::${aws_s3_bucket.iplz_bucket.id}/*"
]
},
{
Effect = "Allow"
Action = [
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
Resource = "*"
}
]
})
}