/
create_internet_gateway.go
74 lines (62 loc) · 1.46 KB
/
create_internet_gateway.go
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
package infrastructure
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
type InternetGateway struct {
ID string `json:"id"`
IsAttachedToVPC bool `json:"is_attached_to_vpc"`
}
func CreateInternetGateway(
ec2Client *ec2.Client,
name string,
) (returnedIG *InternetGateway, returnedError error) {
createInternetGatewayResp, err := ec2Client.CreateInternetGateway(
context.TODO(),
&ec2.CreateInternetGatewayInput{
TagSpecifications: []types.TagSpecification{{
ResourceType: types.ResourceTypeInternetGateway,
Tags: []types.Tag{{
Key: aws.String("Name"),
Value: &name,
}},
}},
},
)
if err != nil {
returnedError = err
return
}
defer func() {
if returnedError == nil {
return
}
_ = RemoveInternetGateway(
ec2Client,
*createInternetGatewayResp.InternetGateway.InternetGatewayId,
)
}()
existsWaiter := ec2.NewInternetGatewayExistsWaiter(ec2Client)
maxWaitTime := 5 * time.Minute
err = existsWaiter.Wait(
context.TODO(),
&ec2.DescribeInternetGatewaysInput{
InternetGatewayIds: []string{
*createInternetGatewayResp.InternetGateway.InternetGatewayId,
},
},
maxWaitTime,
)
if err != nil {
returnedError = err
return
}
returnedIG = &InternetGateway{
ID: *createInternetGatewayResp.InternetGateway.InternetGatewayId,
IsAttachedToVPC: false,
}
return
}