@@ -14,7 +14,9 @@ import (
1414
1515//ResourceMachineCreate creates AWS instance
1616func ResourceMachineCreate (ctx context.Context , d * schema.ResourceData , m interface {}) error {
17+ userData := d .Get ("startup_script" ).(string )
1718 instanceName := d .Get ("name" ).(string )
19+ pairName := d .Id ()
1820 hddSize := d .Get ("instance_hdd_size" ).(int )
1921 region := getRegion (d .Get ("region" ).(string ))
2022 instanceType := getInstanceType (d .Get ("instance_type" ).(string ), d .Get ("instance_gpu" ).(string ))
@@ -57,13 +59,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
5759 })
5860
5961 instanceAmi := * imagesRes .Images [0 ].ImageId
60- pairName := instanceName
6162
6263 // key-pair
6364 svc .ImportKeyPair (& ec2.ImportKeyPairInput {
6465 KeyName : aws .String (pairName ),
6566 PublicKeyMaterial : []byte (keyPublic ),
6667 })
68+ //if err != nil {
69+ //return err
70+ //}
6771
6872 // securityGroup
6973 var vpcID , sgID string
@@ -128,6 +132,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
128132
129133 //launch instance
130134 runResult , err := svc .RunInstancesWithContext (ctx , & ec2.RunInstancesInput {
135+ UserData : aws .String (userData ),
131136 ImageId : aws .String (instanceAmi ),
132137 KeyName : aws .String (pairName ),
133138 InstanceType : aws .String (instanceType ),
@@ -163,6 +168,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
163168 Key : aws .String ("Name" ),
164169 Value : aws .String (instanceName ),
165170 },
171+ {
172+ Key : aws .String ("Id" ),
173+ Value : aws .String (d .Id ()),
174+ },
166175 },
167176 })
168177 if err != nil {
@@ -182,43 +191,50 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
182191 svc .WaitUntilInstanceExistsWithContext (ctx , & statusInput )
183192
184193 descResult , err := svc .DescribeInstancesWithContext (ctx , & statusInput )
185- instanceDesc := descResult .Reservations [0 ].Instances [0 ]
186-
187- d .SetId (instanceID )
188- d .Set ("instance_id" , instanceID )
189- d .Set ("instance_ip" , instanceDesc .PublicIpAddress )
190- d .Set ("instance_launch_time" , instanceDesc .LaunchTime .Format (time .RFC3339 ))
191-
192- d .Set ("key_name" , pairName )
193-
194194 if err != nil {
195195 return err
196196 }
197197
198+ instanceDesc := descResult .Reservations [0 ].Instances [0 ]
199+ d .Set ("instance_ip" , instanceDesc .PublicIpAddress )
200+ d .Set ("instance_launch_time" , instanceDesc .LaunchTime .Format (time .RFC3339 ))
201+
198202 return nil
199203}
200204
201205//ResourceMachineDelete deletes AWS instance
202206func ResourceMachineDelete (ctx context.Context , d * schema.ResourceData , m interface {}) error {
203- svc , _ := awsClient ( getRegion ( d . Get ( "region" ).( string ) ))
204- instanceID := d .Get ("instance_id " ).(string )
207+ id := aws . String ( d . Id ( ))
208+ region := getRegion ( d .Get ("region " ).(string ) )
205209
206- /*
207- pairName := d.Get("key_name").(string)
208- svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
209- KeyName: aws.String(pairName),
210- })
211- */
210+ svc , err := awsClient (region )
211+ if err != nil {
212+ return err
213+ }
212214
213- input := & ec2.TerminateInstancesInput {
214- InstanceIds : []* string {
215- aws .String (instanceID ),
215+ descResult , err := svc .DescribeInstancesWithContext (ctx , & ec2.DescribeInstancesInput {
216+ Filters : []* ec2.Filter {
217+ {
218+ Name : aws .String ("tag:Id" ),
219+ Values : []* string {id },
220+ },
216221 },
217- DryRun : aws .Bool (false ),
222+ })
223+ if err != nil {
224+ return err
218225 }
219226
220- _ , err := svc .TerminateInstances (input )
227+ svc .DeleteKeyPair (& ec2.DeleteKeyPairInput {
228+ KeyName : id ,
229+ })
221230
231+ instanceID := * descResult .Reservations [0 ].Instances [0 ].InstanceId
232+ _ , err = svc .TerminateInstances (& ec2.TerminateInstancesInput {
233+ InstanceIds : []* string {
234+ aws .String (instanceID ),
235+ },
236+ DryRun : aws .Bool (false ),
237+ })
222238 if err != nil {
223239 return err
224240 }
0 commit comments