Skip to content

Commit f9140d4

Browse files
committed
Add flag to optionally upload CF template to s3 bucket
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
1 parent db31cbe commit f9140d4

File tree

5 files changed

+104
-35
lines changed

5 files changed

+104
-35
lines changed

go.mod

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ require (
88
github.com/alecthomas/kingpin/v2 v2.4.0
99
github.com/apparentlymart/go-cidr v1.1.0
1010
github.com/aws/aws-sdk-go-v2 v1.31.0
11-
github.com/aws/aws-sdk-go-v2/config v1.27.38
12-
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.2
13-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.179.1
14-
github.com/aws/smithy-go v1.21.0
11+
github.com/aws/aws-sdk-go-v2/config v1.27.40
12+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.27
13+
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.4
14+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.180.0
15+
github.com/aws/aws-sdk-go-v2/service/s3 v1.64.1
16+
github.com/aws/smithy-go v1.22.0
1517
github.com/crewjam/go-cloudformation v0.0.0-20180605015303-38e5b663797c
1618
github.com/google/uuid v1.6.0
1719
github.com/pkg/errors v0.9.1
@@ -24,17 +26,21 @@ require (
2426
)
2527

2628
require (
27-
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
28-
github.com/aws/aws-sdk-go-v2/credentials v1.17.36 // indirect
29+
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
30+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5 // indirect
31+
github.com/aws/aws-sdk-go-v2/credentials v1.17.38 // indirect
2932
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 // indirect
3033
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 // indirect
3134
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 // indirect
3235
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
36+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.18 // indirect
3337
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5 // indirect
38+
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.20 // indirect
3439
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20 // indirect
35-
github.com/aws/aws-sdk-go-v2/service/sso v1.23.2 // indirect
36-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.2 // indirect
37-
github.com/aws/aws-sdk-go-v2/service/sts v1.31.2 // indirect
40+
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.18 // indirect
41+
github.com/aws/aws-sdk-go-v2/service/sso v1.23.4 // indirect
42+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.4 // indirect
43+
github.com/aws/aws-sdk-go-v2/service/sts v1.31.4 // indirect
3844
github.com/beorn7/perks v1.0.1 // indirect
3945
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4046
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect

go.sum

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
22
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
3-
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg=
4-
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
3+
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b h1:mimo19zliBX/vSQ6PWWSL9lK8qwHozUj03+zLoEB8O0=
4+
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
55
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
66
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
77
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
88
github.com/aws/aws-sdk-go-v2 v1.31.0/go.mod h1:ztolYtaEUtdpf9Wftr31CJfLVjOnD/CVRkKOOYgF8hA=
9-
github.com/aws/aws-sdk-go-v2/config v1.27.38 h1:mMVyJJuSUdbD4zKXoxDgWrgM60QwlFEg+JhihCq6wCw=
10-
github.com/aws/aws-sdk-go-v2/config v1.27.38/go.mod h1:6xOiNEn58bj/64MPKx89r6G/el9JZn8pvVbquSqTKK4=
11-
github.com/aws/aws-sdk-go-v2/credentials v1.17.36 h1:zwI5WrT+oWWfzSKoTNmSyeBKQhsFRJRv+PGW/UZW+Yk=
12-
github.com/aws/aws-sdk-go-v2/credentials v1.17.36/go.mod h1:3AG/sY1rc9NJrNWcN/3KPU4SIDPGTrd/qegKB0TnFdE=
9+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5 h1:xDAuZTn4IMm8o1LnBZvmrL8JA1io4o3YWNXgohbf20g=
10+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5/go.mod h1:wYSv6iDS621sEFLfKvpPE2ugjTuGlAG7iROg0hLOkfc=
11+
github.com/aws/aws-sdk-go-v2/config v1.27.40 h1:sie4mPBGFOO+Z27+yHzvyN31G20h/bf2xb5mCbpLv2Q=
12+
github.com/aws/aws-sdk-go-v2/config v1.27.40/go.mod h1:4KW7Aa5tNo+0VHnuLnnE1vPHtwMurlNZNS65IdcewHA=
13+
github.com/aws/aws-sdk-go-v2/credentials v1.17.38 h1:iM90eRhCeZtlkzCNCG1JysOzJXGYf5rx80aD1lUgNDU=
14+
github.com/aws/aws-sdk-go-v2/credentials v1.17.38/go.mod h1:TCVYPZeQuLaYNEkf/TVn6k5k/zdVZZ7xH9po548VNNg=
1315
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 h1:C/d03NAmh8C4BZXhuRNboF/DqhBkBCeDiJDcaqIT5pA=
1416
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14/go.mod h1:7I0Ju7p9mCIdlrfS+JCgqcYD0VXz/N4yozsox+0o078=
17+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.27 h1:1oLpQSTuqbizOUEYdxAwH+Eveg+FOCOkg84Yijba6Kc=
18+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.27/go.mod h1:afo0vF9P3pjy1ny+cb45lzBjtKeEb5t5MPRxeTXpujw=
1519
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 h1:kYQ3H1u0ANr9KEKlGs/jTLrBFPo8P8NaH/w7A01NeeM=
1620
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18/go.mod h1:r506HmK5JDUh9+Mw4CfGJGSSoqIiLCndAuqXuhbv67Y=
1721
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 h1:Z7IdFUONvTcvS7YuhtVxN99v2cCoHRXOS4mTr0B/pUc=
1822
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18/go.mod h1:DkKMmksZVVyat+Y+r1dEOgJEfUeA7UngIHWeKsi0yNc=
1923
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
2024
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
21-
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.2 h1:0xzfXPA31Hkoi9KnQnkZNll3/KvkeYF8eYAvbjy0FOI=
22-
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.2/go.mod h1:85xWVAzH8I6dCauQy7j1nt8CbSELPzGQj45chIZ/qMA=
23-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.179.1 h1:TwFjSwRn1kR1i1qeq5cQBRwRaZ80JQS8BHsJTb6QBk8=
24-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.179.1/go.mod h1:W6sNzs5T4VpZn1Vy+FMKw8s24vt5k6zPJXcNOK0asBo=
25+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.18 h1:OWYvKL53l1rbsUmW7bQyJVsYU/Ii3bbAAQIIFNbM0Tk=
26+
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.18/go.mod h1:CUx0G1v3wG6l01tUB+j7Y8kclA8NSqK4ef0YG79a4cg=
27+
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.4 h1:5bV0zoOzqn8tdmgrdgdnHPAtLiVwswNj9pgtkqRMj9E=
28+
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.4/go.mod h1:85xWVAzH8I6dCauQy7j1nt8CbSELPzGQj45chIZ/qMA=
29+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.180.0 h1:Tr9jEshJlWcS+pgXYh09SsHeX1eqKXTfoNEoTSCPNxI=
30+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.180.0/go.mod h1:W6sNzs5T4VpZn1Vy+FMKw8s24vt5k6zPJXcNOK0asBo=
2531
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5 h1:QFASJGfT8wMXtuP3D5CRmMjARHv9ZmzFUMJznHDOY3w=
2632
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5/go.mod h1:QdZ3OmoIjSX+8D1OPAzPxDfjXASbBMDsz9qvtyIhtik=
33+
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.20 h1:rTWjG6AvWekO2B1LHeM3ktU7MqyX9rzWQ7hgzneZW7E=
34+
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.20/go.mod h1:RGW2DDpVc8hu6Y6yG8G5CHVmVOAn1oV8rNKOHRJyswg=
2735
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20 h1:Xbwbmk44URTiHNx6PNo0ujDE6ERlsCKJD3u1zfnzAPg=
2836
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20/go.mod h1:oAfOFzUB14ltPZj1rWwRc3d/6OgD76R8KlvU3EqM9Fg=
29-
github.com/aws/aws-sdk-go-v2/service/sso v1.23.2 h1:yzi/y/vKlLyzOfG7pSu5ONNGRxHIgLeDrV4w2AMRCo0=
30-
github.com/aws/aws-sdk-go-v2/service/sso v1.23.2/go.mod h1:XRlMvmad0ZNL+75C5FYdMvbbLkd6qiqz6foR1nA1PXY=
31-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.2 h1:3gb6pYhYLjo8rB1h2Tqs61wpjRd3rQymYcVq/pp0yxI=
32-
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.2/go.mod h1:FnvDM4sfa+isJ3kDXIzAB9GAwVSzFzSy97uZ3IsHo4E=
33-
github.com/aws/aws-sdk-go-v2/service/sts v1.31.2 h1:O6tyji8mXmBGsHvTCB0VIhrDw19lGTUSbKIyjnw79s8=
34-
github.com/aws/aws-sdk-go-v2/service/sts v1.31.2/go.mod h1:yMWe0F+XG0DkRZK5ODZhG7BEFYhLXi2dqGsv6tX0cgI=
35-
github.com/aws/smithy-go v1.21.0 h1:H7L8dtDRk0P1Qm6y0ji7MCYMQObJ5R9CRpyPhRUkLYA=
36-
github.com/aws/smithy-go v1.21.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
37+
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.18 h1:eb+tFOIl9ZsUe2259/BKPeniKuz4/02zZFH/i4Nf8Rg=
38+
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.18/go.mod h1:GVCC2IJNJTmdlyEsSmofEy7EfJncP7DNnXDzRjJ5Keg=
39+
github.com/aws/aws-sdk-go-v2/service/s3 v1.64.1 h1:jjHf+M6vCp/WzbyFEroY4/Nx8dJac520A0EPwlYk0Do=
40+
github.com/aws/aws-sdk-go-v2/service/s3 v1.64.1/go.mod h1:NLTqRLe3pUNu3nTEHI6XlHLKYmc8fbHUdMxAB6+s41Q=
41+
github.com/aws/aws-sdk-go-v2/service/sso v1.23.4 h1:ck/Y8XWNR1gHa4BFkwE3oSu7XDJGwl+8TI7E/RB2EcQ=
42+
github.com/aws/aws-sdk-go-v2/service/sso v1.23.4/go.mod h1:XRlMvmad0ZNL+75C5FYdMvbbLkd6qiqz6foR1nA1PXY=
43+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.4 h1:4f2/JKYZHAZbQ7koBpZ012bKi32NHPY0m7TDuJgsbug=
44+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.4/go.mod h1:FnvDM4sfa+isJ3kDXIzAB9GAwVSzFzSy97uZ3IsHo4E=
45+
github.com/aws/aws-sdk-go-v2/service/sts v1.31.4 h1:uK6dUUdJtqutK1XO/tmNaQMJiPLCJY/eAeOOmqQ6ygY=
46+
github.com/aws/aws-sdk-go-v2/service/sts v1.31.4/go.mod h1:yMWe0F+XG0DkRZK5ODZhG7BEFYhLXi2dqGsv6tX0cgI=
47+
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
48+
github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
3749
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
3850
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
3951
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Config struct {
4242
LogLevel string
4343
Provider string
4444
VPCID string
45+
CFTemplateBucket string
4546
ClusterID string
4647
ClusterIDTagPrefix string
4748
ControllerID string
@@ -78,20 +79,19 @@ func NewConfig() *Config {
7879
}
7980
}
8081

81-
func newProvider(clusterID, controllerID string, dry bool, name, vpcID string, clusterIDTagPrefix string, natCidrBlocks, availabilityZones []string, stackTerminationProtection bool, additionalStackTags map[string]string) (provider.Provider, error) {
82+
func newProvider(clusterID, controllerID string, dry bool, name, vpcID string, cfTemplateBucket string, clusterIDTagPrefix string, natCidrBlocks, availabilityZones []string, stackTerminationProtection bool, additionalStackTags map[string]string) (provider.Provider, error) {
8283
switch name {
8384
case aws.ProviderName:
8485
cfg, err := config.LoadDefaultConfig(context.TODO())
8586
if err != nil {
8687
return nil, err
8788
}
88-
return aws.NewAWSProvider(cfg, clusterID, controllerID, dry, vpcID, clusterIDTagPrefix, natCidrBlocks, availabilityZones, stackTerminationProtection, additionalStackTags)
89+
return aws.NewAWSProvider(cfg, clusterID, controllerID, dry, vpcID, cfTemplateBucket, clusterIDTagPrefix, natCidrBlocks, availabilityZones, stackTerminationProtection, additionalStackTags)
8990
case noop.ProviderName:
9091
return noop.NewNoopProvider(), nil
9192
default:
9293
return nil, fmt.Errorf("Unkown provider: %s", name)
9394
}
94-
return nil, nil
9595
}
9696

9797
func allLogLevelsAsStrings() []string {
@@ -125,6 +125,7 @@ Example:
125125
app.Flag("cluster-id-tag-prefix", "Prefix for the Cluster ID tag set on the Egress stack.").Default(defaultConfig.ClusterIDTagPrefix).StringVar(&cfg.ClusterIDTagPrefix)
126126
app.Flag("controller-id", "Controller ID used to identify ownership of Egress stack.").Default(defaultConfig.ControllerID).StringVar(&cfg.ControllerID)
127127
app.Flag("vpc-id", "VPC ID (default: auto-detect)").Default(defaultConfig.VPCID).StringVar(&cfg.VPCID)
128+
app.Flag("cf-template-bucket", "S3 bucket to use for CF template storage").StringVar(&cfg.CFTemplateBucket)
128129
app.Flag("aws-nat-cidr-block", "AWS Provider requires to specify NAT-CIDR-Blocks for each AZ to have a NAT gateway in. Each should be a small network having only the NAT GW").StringsVar(&cfg.NatCidrBlocks)
129130
app.Flag("aws-az", "AWS Provider requires to specify all AZs to have a NAT gateway in.").StringsVar(&cfg.AvailabilityZones)
130131
app.Flag("stack-termination-protection", "Enables AWS clouformation stack termination protection for the stacks managed by the controller.").BoolVar(&cfg.StackTerminationProtection)
@@ -162,7 +163,7 @@ func main() {
162163
log.SetLevel(ll)
163164
log.Debugf("config: %+v", cfg)
164165

165-
p, err := newProvider(cfg.ClusterID, cfg.ControllerID, cfg.DryRun, cfg.Provider, cfg.VPCID, cfg.ClusterIDTagPrefix, cfg.NatCidrBlocks, cfg.AvailabilityZones, cfg.StackTerminationProtection, cfg.AdditionalStackTags)
166+
p, err := newProvider(cfg.ClusterID, cfg.ControllerID, cfg.DryRun, cfg.Provider, cfg.VPCID, cfg.CFTemplateBucket, cfg.ClusterIDTagPrefix, cfg.NatCidrBlocks, cfg.AvailabilityZones, cfg.StackTerminationProtection, cfg.AdditionalStackTags)
166167
if err != nil {
167168
log.Fatalf("Failed to create provider: %v", err)
168169
}

provider/aws/aws.go

+48-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111

1212
"github.com/aws/aws-sdk-go-v2/aws"
1313
"github.com/aws/aws-sdk-go-v2/aws/transport/http"
14+
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
1415
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
1516
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
1617
"github.com/aws/aws-sdk-go-v2/service/ec2"
1718
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
19+
"github.com/aws/aws-sdk-go-v2/service/s3"
1820
"github.com/aws/smithy-go"
1921
cft "github.com/crewjam/go-cloudformation"
2022
"github.com/pkg/errors"
@@ -52,10 +54,12 @@ type AWSProvider struct {
5254
controllerID string
5355
dry bool
5456
vpcID string
57+
cfTemplateBucket string
5558
natCidrBlocks []string
5659
availabilityZones []string
5760
cloudformation cloudformationAPI
5861
ec2 ec2API
62+
s3Uploader s3UploaderAPI
5963
stackTerminationProtection bool
6064
additionalStackTags map[string]string
6165
logger *log.Entry
@@ -72,18 +76,20 @@ type stackSpec struct {
7276
tags []cftypes.Tag
7377
}
7478

75-
func NewAWSProvider(cfg aws.Config, clusterID, controllerID string, dry bool, vpcID string, clusterIDTagPrefix string, natCidrBlocks, availabilityZones []string, stackTerminationProtection bool, additionalStackTags map[string]string) (*AWSProvider, error) {
79+
func NewAWSProvider(cfg aws.Config, clusterID, controllerID string, dry bool, vpcID string, cfTemplateBucket string, clusterIDTagPrefix string, natCidrBlocks, availabilityZones []string, stackTerminationProtection bool, additionalStackTags map[string]string) (*AWSProvider, error) {
7680
// TODO: find vpcID at startup
7781
return &AWSProvider{
7882
clusterID: clusterID,
7983
clusterIDTagPrefix: clusterIDTagPrefix,
8084
controllerID: controllerID,
8185
dry: dry,
8286
vpcID: vpcID,
87+
cfTemplateBucket: cfTemplateBucket,
8388
natCidrBlocks: natCidrBlocks,
8489
availabilityZones: availabilityZones,
8590
cloudformation: cloudformation.NewFromConfig(cfg),
8691
ec2: ec2.NewFromConfig(cfg),
92+
s3Uploader: manager.NewUploader(s3.NewFromConfig(cfg)),
8793
stackTerminationProtection: stackTerminationProtection,
8894
additionalStackTags: additionalStackTags,
8995
logger: log.WithFields(log.Fields{"provider": ProviderName}),
@@ -515,6 +521,20 @@ func (p *AWSProvider) deleteCFStack(ctx context.Context, stackName string) error
515521
}
516522

517523
func (p *AWSProvider) updateCFStack(ctx context.Context, spec *stackSpec) error {
524+
var templateURL string
525+
if p.cfTemplateBucket != "" {
526+
// Upload the stack template to S3
527+
result, err := p.s3Uploader.Upload(ctx, &s3.PutObjectInput{
528+
Bucket: aws.String(p.cfTemplateBucket),
529+
Key: aws.String(fmt.Sprintf("%s.template", spec.name)),
530+
Body: strings.NewReader(spec.template),
531+
})
532+
if err != nil {
533+
return err
534+
}
535+
templateURL = result.Location
536+
}
537+
518538
params := &cloudformation.UpdateStackInput{
519539
StackName: aws.String(spec.name),
520540
Parameters: append(
@@ -524,8 +544,13 @@ func (p *AWSProvider) updateCFStack(ctx context.Context, spec *stackSpec) error
524544
},
525545
routeTableParams(spec)...,
526546
),
527-
TemplateBody: aws.String(spec.template),
528-
Tags: spec.tags,
547+
Tags: spec.tags,
548+
}
549+
550+
if templateURL != "" {
551+
params.TemplateURL = aws.String(templateURL)
552+
} else {
553+
params.TemplateBody = aws.String(spec.template)
529554
}
530555

531556
if !p.dry {
@@ -561,6 +586,20 @@ func (p *AWSProvider) updateCFStack(ctx context.Context, spec *stackSpec) error
561586
}
562587

563588
func (p *AWSProvider) createCFStack(ctx context.Context, spec *stackSpec) error {
589+
var templateURL string
590+
if p.cfTemplateBucket != "" {
591+
// Upload the stack template to S3
592+
result, err := p.s3Uploader.Upload(ctx, &s3.PutObjectInput{
593+
Bucket: aws.String(p.cfTemplateBucket),
594+
Key: aws.String(fmt.Sprintf("%s.template", spec.name)),
595+
Body: strings.NewReader(spec.template),
596+
})
597+
if err != nil {
598+
return err
599+
}
600+
templateURL = result.Location
601+
}
602+
564603
params := &cloudformation.CreateStackInput{
565604
StackName: aws.String(spec.name),
566605
OnFailure: cftypes.OnFailureDelete,
@@ -571,12 +610,17 @@ func (p *AWSProvider) createCFStack(ctx context.Context, spec *stackSpec) error
571610
},
572611
routeTableParams(spec)...,
573612
),
574-
TemplateBody: aws.String(spec.template),
575613
TimeoutInMinutes: aws.Int32(int32(spec.timeoutInMinutes)),
576614
EnableTerminationProtection: aws.Bool(spec.stackTerminationProtection),
577615
Tags: spec.tags,
578616
}
579617

618+
if templateURL != "" {
619+
params.TemplateURL = aws.String(templateURL)
620+
} else {
621+
params.TemplateBody = aws.String(spec.template)
622+
}
623+
580624
if !p.dry {
581625
_, err := p.cloudformation.CreateStack(ctx, params)
582626
if err != nil {

provider/aws/iface.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package aws
33
import (
44
"context"
55

6+
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
67
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
78
"github.com/aws/aws-sdk-go-v2/service/ec2"
9+
"github.com/aws/aws-sdk-go-v2/service/s3"
810
)
911

1012
type cloudformationAPI interface {
@@ -21,3 +23,7 @@ type ec2API interface {
2123
DescribeVpcs(ctx context.Context, params *ec2.DescribeVpcsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeVpcsOutput, error)
2224
DescribeRouteTables(context.Context, *ec2.DescribeRouteTablesInput, ...func(*ec2.Options)) (*ec2.DescribeRouteTablesOutput, error)
2325
}
26+
27+
type s3UploaderAPI interface {
28+
Upload(ctx context.Context, input *s3.PutObjectInput, opts ...func(*manager.Uploader)) (*manager.UploadOutput, error)
29+
}

0 commit comments

Comments
 (0)