@@ -26,7 +26,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
26
26
instanceName := d .Get ("name" ).(string )
27
27
pairName := d .Id ()
28
28
hddSize := d .Get ("instance_hdd_size" ).(int )
29
- region := getRegion (d .Get ("region" ).(string ))
29
+ region := GetRegion (d .Get ("region" ).(string ))
30
30
instanceType := getInstanceType (d .Get ("instance_type" ).(string ), d .Get ("instance_gpu" ).(string ))
31
31
ami := d .Get ("image" ).(string )
32
32
keyPublic := d .Get ("ssh_public" ).(string )
@@ -38,7 +38,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
38
38
}
39
39
40
40
svc , err := awsClient (region )
41
-
42
41
if err != nil {
43
42
return decodeAWSError (region , err )
44
43
}
@@ -70,6 +69,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
70
69
Name : aws .String ("architecture" ),
71
70
Values : []* string {aws .String ("x86_64" )},
72
71
},
72
+ {
73
+ Name : aws .String ("owner-id" ),
74
+ Values : []* string {aws .String ("099720109477" )},
75
+ },
73
76
},
74
77
})
75
78
@@ -101,11 +104,19 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
101
104
securityGroup = "iterative"
102
105
103
106
vpcsDesc , _ := svc .DescribeVpcs (& ec2.DescribeVpcsInput {})
107
+
104
108
if len (vpcsDesc .Vpcs ) == 0 {
105
109
return errors .New ("no VPCs found" )
106
110
}
107
111
vpcID = * vpcsDesc .Vpcs [0 ].VpcId
108
112
113
+ for _ , vpc := range vpcsDesc .Vpcs {
114
+ if * vpc .IsDefault {
115
+ vpcID = * vpc .VpcId
116
+ break
117
+ }
118
+ }
119
+
109
120
gpResult , err := svc .CreateSecurityGroup (& ec2.CreateSecurityGroupInput {
110
121
GroupName : aws .String (securityGroup ),
111
122
Description : aws .String ("Iterative security group" ),
@@ -177,6 +188,16 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
177
188
if len (subDesc .Subnets ) == 0 {
178
189
return errors .New ("no subnets found" )
179
190
}
191
+ var subnetID string
192
+ for _ , subnet := range subDesc .Subnets {
193
+ if * subnet .AvailableIpAddressCount > 0 && * subnet .MapPublicIpOnLaunch {
194
+ subnetID = * subnet .SubnetId
195
+ break
196
+ }
197
+ }
198
+ if subnetID == "" {
199
+ return errors .New ("No subnet found with public IPs available or able to create new public IPs on creation" )
200
+ }
180
201
181
202
blockDeviceMappings := []* ec2.BlockDeviceMapping {
182
203
{
@@ -200,7 +221,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
200
221
KeyName : aws .String (pairName ),
201
222
InstanceType : aws .String (instanceType ),
202
223
SecurityGroupIds : []* string {aws .String (sgID )},
203
- SubnetId : aws .String (* subDesc . Subnets [ 0 ]. SubnetId ),
224
+ SubnetId : aws .String (subnetID ),
204
225
BlockDeviceMappings : blockDeviceMappings ,
205
226
},
206
227
InstanceCount : aws .Int64 (1 ),
@@ -287,14 +308,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
287
308
instanceDesc := descResult .Reservations [0 ].Instances [0 ]
288
309
d .Set ("instance_ip" , instanceDesc .PublicIpAddress )
289
310
d .Set ("instance_launch_time" , instanceDesc .LaunchTime .Format (time .RFC3339 ))
311
+ d .Set ("image" , * imagesRes .Images [0 ].Name )
290
312
291
313
return nil
292
314
}
293
315
294
316
//ResourceMachineDelete deletes AWS instance
295
317
func ResourceMachineDelete (ctx context.Context , d * schema.ResourceData , m interface {}) error {
296
318
id := aws .String (d .Id ())
297
- region := getRegion (d .Get ("region" ).(string ))
319
+ region := GetRegion (d .Get ("region" ).(string ))
298
320
299
321
svc , err := awsClient (region )
300
322
if err != nil {
@@ -359,7 +381,8 @@ var ImageRegions = []string{
359
381
"sa-east-1" ,
360
382
}
361
383
362
- func getRegion (region string ) string {
384
+ //GetRegion maps region to real cloud regions
385
+ func GetRegion (region string ) string {
363
386
instanceRegions := make (map [string ]string )
364
387
instanceRegions ["us-east" ] = "us-east-1"
365
388
instanceRegions ["us-west" ] = "us-west-1"
0 commit comments