Skip to content

Commit

Permalink
Merge pull request #217 from kayac/cfn
Browse files Browse the repository at this point in the history
add cloudformation plugin
  • Loading branch information
fujiwara authored Jan 8, 2021
2 parents 86fee05 + f77304a commit 414abf2
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ service: test
service_definition: ecs-service-def.json
task_definition: ecs-task-def.json
plugins:
- name: tfstate
config:
path: terraform.tfstate # path to tfstate file
- name: tfstate
config:
path: terraform.tfstate # path to tfstate file
```

ecs-service-def.json
Expand All @@ -548,6 +548,54 @@ ecs-service-def.json

`{{ tfstate "resource_type.resource_name.attr" }}` will expand to an attribute value of the resource in tfstate.

## cloudformation

cloudformation plugin introduces template functions `cfn_output` and `cfn_export`.

An example of CloudFormation stack template defines Outputs and Exports.

```yaml
# StackName: ECS-ecspresso
Outputs:
SubnetAz1:
Value: !Ref PublicSubnetAz1
SubnetAz2:
Value: !Ref PublicSubnetAz2
EcsSecurityGroupId:
Value: !Ref EcsSecurityGroup
Export:
Name: !Sub ${AWS::StackName}-EcsSecurityGroupId
```
Load cloudformation plugin in a config file.
config.yaml
```yaml
# ...
plugins:
- name: cloudformation
```
`cfn_output StackName OutputKey` lookups OutputValue of OutputKey in the StackName.
`cfn_export ExportName` lookups exported value by name.

ecs-service-def.json
```json
{
"networkConfiguration": {
"awsvpcConfiguration": {
"subnets": [
"{{ cfn_output `ECS-ecspresso` `SubnetAz1` }}",
"{{ cfn_output `ECS-ecspresso` `SubnetAz2` }}"
],
"securityGroups": [
"{{ cfn_export `ECS-ecspresso-EcsSecurityGroupId` }}"
]
}
}
}
```

# LICENCE

MIT
Expand Down
11 changes: 11 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"text/template"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/fatih/color"
gv "github.com/hashicorp/go-version"
"github.com/kayac/ecspresso/appspec"
Expand Down Expand Up @@ -34,6 +36,7 @@ type Config struct {
templateFuncs []template.FuncMap
dir string
versionConstraints gv.Constraints
sess *session.Session
}

// Load loads configuration file from file path.
Expand Down Expand Up @@ -66,7 +69,15 @@ func (c *Config) Restrict() error {
}
c.versionConstraints = constraints
}
var err error
c.sess, err = session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(c.Region)},
SharedConfigState: session.SharedConfigEnable,
})
return err
}

func (c *Config) setupPlugins() error {
for _, p := range c.Plugins {
if err := p.Setup(c); err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions ecspresso.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/kayac/go-config"
gc "github.com/kayac/go-config"
"github.com/mattn/go-isatty"
"github.com/morikuni/aec"
"github.com/pkg/errors"
Expand Down Expand Up @@ -51,7 +51,7 @@ type App struct {
config *Config
Debug bool

loader *config.Loader
loader *gc.Loader
}

func (d *App) DescribeServicesInput() *ecs.DescribeServicesInput {
Expand Down Expand Up @@ -256,15 +256,15 @@ func (d *App) GetLogEvents(ctx context.Context, logGroup string, logStream strin
}

func NewApp(conf *Config) (*App, error) {
loader := config.New()
if err := conf.setupPlugins(); err != nil {
return nil, err
}
loader := gc.New()
for _, f := range conf.templateFuncs {
loader.Funcs(f)
}

sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(conf.Region)},
SharedConfigState: session.SharedConfigEnable,
}))
sess := conf.sess
d := &App{
Service: conf.Service,
Cluster: conf.Cluster,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.12
require (
github.com/Songmu/prompter v0.4.0
github.com/alecthomas/kingpin v1.3.8-0.20190930021037-0a108b7f5563
github.com/aws/aws-sdk-go v1.35.35
github.com/aws/aws-sdk-go v1.36.15
github.com/fatih/color v1.9.0
github.com/fujiwara/cfn-lookup v0.0.2
github.com/fujiwara/tfstate-lookup v0.0.14
github.com/google/go-cmp v0.5.2
github.com/hashicorp/go-version v1.2.1
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/aws/aws-sdk-go v1.30.7/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.35.35 h1:o/EbgEcIPWga7GWhJhb3tiaxqk4/goTdo5YEMdnVxgE=
github.com/aws/aws-sdk-go v1.35.35/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.36.15 h1:nGqgPlXegCKPZOKXvWnYCLvLPJPRoSOHHn9d0N0DG7Y=
github.com/aws/aws-sdk-go v1.36.15/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fujiwara/cfn-lookup v0.0.2 h1:1l0HNyPMO5yzXacVi6LWbyahs6uW+faBPd8CwFOiaFw=
github.com/fujiwara/cfn-lookup v0.0.2/go.mod h1:if7hlqDFCHxOy2TnH/GWKpD6j5Bp3UwFl+jfj9WZYis=
github.com/fujiwara/tfstate-lookup v0.0.14 h1:cjSRXGRWRq57WCDt5VESFpFBNTQF18eCAv8zv+En260=
github.com/fujiwara/tfstate-lookup v0.0.14/go.mod h1:08o5Rm5pKzvIxoZe3D0NIT8AHYBqTzyUU03BZ/c0IKA=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand All @@ -33,6 +35,7 @@ github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
Expand Down
15 changes: 14 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/fujiwara/cfn-lookup/cfn"
"github.com/fujiwara/tfstate-lookup/tfstate"
)

Expand All @@ -14,9 +16,11 @@ type ConfigPlugin struct {
}

func (p ConfigPlugin) Setup(c *Config) error {
switch p.Name {
switch strings.ToLower(p.Name) {
case "tfstate":
return setupPluginTFState(p, c)
case "cloudformation":
return setupPluginCFn(p, c)
default:
return fmt.Errorf("plugin %s is not available", p.Name)
}
Expand All @@ -37,3 +41,12 @@ func setupPluginTFState(p ConfigPlugin, c *Config) error {
c.templateFuncs = append(c.templateFuncs, funcs)
return nil
}

func setupPluginCFn(p ConfigPlugin, c *Config) error {
funcs, err := cfn.FuncMap(c.sess)
if err != nil {
return err
}
c.templateFuncs = append(c.templateFuncs, funcs)
return nil
}
2 changes: 1 addition & 1 deletion tests/ci/ecs-task-def.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"name": "BAZ",
"valueFrom": "arn:aws:secretsmanager:{{must_env `AWS_REGION`}}:{{must_env `AWS_ACCOUNT_ID`}}:secret:ecspresso-test/baz-06XQOH:bar::"
"valueFrom": "arn:aws:secretsmanager:{{must_env `AWS_REGION`}}:{{must_env `AWS_ACCOUNT_ID`}}:secret:ecspresso-test/baz-06XQOH"
}
],
"name": "nginx",
Expand Down

0 comments on commit 414abf2

Please sign in to comment.