@@ -19,15 +19,23 @@ func resourceMachine() *schema.Resource {
19
19
//UpdateContext: resourceMachineUpdate,s
20
20
DeleteContext : resourceMachineDelete ,
21
21
Schema : map [string ]* schema.Schema {
22
- "key_name " : & schema.Schema {
22
+ "region " : & schema.Schema {
23
23
Type : schema .TypeString ,
24
24
Optional : true ,
25
- Computed : true ,
25
+ ForceNew : true ,
26
+ Default : "us-west-2" ,
26
27
},
27
- "private_key " : & schema.Schema {
28
+ "instance_ami " : & schema.Schema {
28
29
Type : schema .TypeString ,
29
30
Optional : true ,
30
- Computed : true ,
31
+ ForceNew : true ,
32
+ Default : "ami-e7527ed7" ,
33
+ },
34
+ "instance_type" : & schema.Schema {
35
+ Type : schema .TypeString ,
36
+ Optional : true ,
37
+ ForceNew : true ,
38
+ Default : "t2.micro" ,
31
39
},
32
40
"instance_id" : & schema.Schema {
33
41
Type : schema .TypeString ,
@@ -44,23 +52,27 @@ func resourceMachine() *schema.Resource {
44
52
Optional : true ,
45
53
Computed : true ,
46
54
},
47
- "instance_ami " : & schema.Schema {
55
+ "key_name " : & schema.Schema {
48
56
Type : schema .TypeString ,
49
57
Optional : true ,
50
- ForceNew : true ,
51
- Default : "ami-e7527ed7" ,
58
+ Computed : true ,
52
59
},
53
- "instance_type " : & schema.Schema {
60
+ "key_public " : & schema.Schema {
54
61
Type : schema .TypeString ,
55
62
Optional : true ,
56
63
ForceNew : true ,
57
- Default : "t2.micro " ,
64
+ Default : "" ,
58
65
},
59
- "region" : & schema.Schema {
66
+ "key_private" : & schema.Schema {
67
+ Type : schema .TypeString ,
68
+ Optional : true ,
69
+ Computed : true ,
70
+ },
71
+ "aws_security_group" : & schema.Schema {
60
72
Type : schema .TypeString ,
61
73
Optional : true ,
62
74
ForceNew : true ,
63
- Default : "us-west-2 " ,
75
+ Default : "" ,
64
76
},
65
77
},
66
78
}
@@ -69,42 +81,56 @@ func resourceMachine() *schema.Resource {
69
81
func resourceMachineCreate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
70
82
var diags diag.Diagnostics
71
83
84
+ svc , errClient := awsClient (d )
85
+ if errClient != nil {
86
+ return diag .FromErr (errClient )
87
+ }
88
+
72
89
sid , err := shortid .New (1 , shortid .DefaultABC , 2342 )
73
90
id , _ := sid .Generate ()
74
91
75
- svc , _ := awsClient (d )
76
- ctxx := context .Background ()
77
-
78
- ami := d .Get ("instance_ami" ).(string )
92
+ instanceAmi := d .Get ("instance_ami" ).(string )
79
93
instanceType := d .Get ("instance_type" ).(string )
94
+ keyPublic := d .Get ("key_public" ).(string )
95
+
96
+ securityGroup := d .Get ("aws_security_group" ).(string )
97
+
80
98
pairName := "cml_" + id
81
- groupName := "cml"
99
+ var keyMaterial string
82
100
83
- keyResult , err := svc .CreateKeyPair (& ec2.CreateKeyPairInput {
84
- KeyName : aws .String (pairName ),
85
- })
86
- if err != nil {
87
- return diag .FromErr (err )
101
+ // key-pair
102
+ if len (keyPublic ) != 0 {
103
+ _ , errImportKeyPair := svc .ImportKeyPair (& ec2.ImportKeyPairInput {
104
+ KeyName : aws .String (pairName ),
105
+ PublicKeyMaterial : []byte (keyPublic ),
106
+ })
107
+ if errImportKeyPair != nil {
108
+ return diag .FromErr (errImportKeyPair )
109
+ }
110
+
111
+ } else {
112
+ keyResult , err := svc .CreateKeyPair (& ec2.CreateKeyPairInput {
113
+ KeyName : aws .String (pairName ),
114
+ })
115
+ if err != nil {
116
+ return diag .FromErr (err )
117
+ }
118
+ keyMaterial = * keyResult .KeyMaterial
88
119
}
89
- keyMaterial := * keyResult .KeyMaterial
90
120
91
- vpcsDesc , _ := svc .DescribeVpcs (& ec2.DescribeVpcsInput {
92
- /* Filters: []*ec2.Filter{
93
- {
94
- Name: aws.String("tag:Name"),
95
- Values: []*string{aws.String("cml")},
96
- },
97
- }, */
98
- })
99
- vpc := vpcsDesc .Vpcs [0 ]
121
+ if len (securityGroup ) == 0 {
122
+ securityGroup = "cml"
100
123
101
- gpResult , ee := svc .CreateSecurityGroup (& ec2.CreateSecurityGroupInput {
102
- GroupName : aws .String (groupName ),
103
- Description : aws .String ("CML security group" ),
104
- VpcId : aws .String (* vpc .VpcId ),
105
- })
124
+ vpcsDesc , _ := svc .DescribeVpcs (& ec2.DescribeVpcsInput {})
125
+ vpc := vpcsDesc .Vpcs [0 ]
126
+ vpcID := * vpc .VpcId
127
+
128
+ gpResult , ee := svc .CreateSecurityGroup (& ec2.CreateSecurityGroupInput {
129
+ GroupName : aws .String (securityGroup ),
130
+ Description : aws .String ("CML security group" ),
131
+ VpcId : aws .String (vpcID ),
132
+ })
106
133
107
- if ee == nil {
108
134
svc .AuthorizeSecurityGroupIngress (& ec2.AuthorizeSecurityGroupIngressInput {
109
135
GroupId : aws .String (* gpResult .GroupId ),
110
136
IpPermissions : []* ec2.IpPermission {
@@ -130,18 +156,20 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
130
156
}),
131
157
},
132
158
})
159
+ if ee != nil {
160
+ return diag .FromErr (err )
161
+ }
133
162
}
134
163
135
- runResult , err := svc .RunInstancesWithContext (ctxx , & ec2.RunInstancesInput {
164
+ runResult , err := svc .RunInstancesWithContext (ctx , & ec2.RunInstancesInput {
165
+ ImageId : aws .String (instanceAmi ),
136
166
KeyName : aws .String (pairName ),
137
- ImageId : aws .String (ami ),
138
167
InstanceType : aws .String (instanceType ),
139
168
MinCount : aws .Int64 (1 ),
140
169
MaxCount : aws .Int64 (1 ),
141
170
SecurityGroups : []* string {
142
- aws .String (groupName ),
171
+ aws .String (securityGroup ),
143
172
},
144
-
145
173
//CpuOptions: instanceOpts.CpuOptions,
146
174
})
147
175
if err != nil {
@@ -169,24 +197,24 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
169
197
instanceIds [0 ] = & instanceID
170
198
statusInput := ec2.DescribeInstancesInput {
171
199
InstanceIds : instanceIds ,
200
+ Filters : []* ec2.Filter {
201
+ {
202
+ Name : aws .String ("instance-state-name" ),
203
+ Values : []* string {aws .String ("running" )},
204
+ },
205
+ },
172
206
}
173
- svc .WaitUntilInstanceExistsWithContext (ctxx , & statusInput )
207
+ svc .WaitUntilInstanceExistsWithContext (ctx , & statusInput )
174
208
175
- time .Sleep (50 * time .Second )
176
-
177
- descResult , _ := svc .DescribeInstancesWithContext (ctxx , & statusInput )
209
+ descResult , _ := svc .DescribeInstancesWithContext (ctx , & statusInput )
178
210
instanceDesc := descResult .Reservations [0 ].Instances [0 ]
179
211
180
212
d .SetId (instanceID )
181
213
d .Set ("instance_id" , instanceID )
182
214
d .Set ("instance_ip" , instanceDesc .PublicIpAddress )
183
215
d .Set ("instance_launch_time" , instanceDesc .LaunchTime .Format (time .RFC3339 ))
184
216
d .Set ("key_name" , pairName )
185
- d .Set ("private_key" , keyMaterial )
186
-
187
- /* if err := d.Set("instaceID", instanceID); err != nil {
188
- return diag.FromErr(err)
189
- } */
217
+ d .Set ("key_privates" , keyMaterial )
190
218
191
219
return diags
192
220
}
@@ -230,14 +258,12 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
230
258
return diags
231
259
}
232
260
233
- func awsClient (d * schema.ResourceData ) (* ec2.EC2 , diag.Diagnostics ) {
234
- var diags diag.Diagnostics
235
-
261
+ func awsClient (d * schema.ResourceData ) (* ec2.EC2 , error ) {
236
262
region := d .Get ("region" ).(string )
237
- sess , _ := session .NewSession (& aws.Config {
263
+ sess , err := session .NewSession (& aws.Config {
238
264
Region : aws .String (region )},
239
265
)
240
266
svc := ec2 .New (sess )
241
267
242
- return svc , diags
268
+ return svc , err
243
269
}
0 commit comments