Create a gist now

Instantly share code, notes, and snippets.

@dekokun /LICENSE
Last active Apr 17, 2017

What would you like to do?

Usage

  1. aws_ip_cloudfront.goとhoge.tmplを同じディレクトリに置く

  2. go get github.com/bhendo/awsipranges みたいな感じで bhendo/awsipranges をインストール

  3. hoge.tmplの cidrs, err := a.PrefixesByService("EC2") の部分を EC2CLOUDFRONT 等のいい感じの名前に変える

  4. 自環境の既存のVPCのID の部分を既存のVPCのIDに変える

  5. 以下叩くと標準出力にtemplateが出力される。めでたしめでたし

go run aws_ip_cloudfront.go
package main
import (
"bytes"
"fmt"
"github.com/bhendo/awsipranges"
"net/http"
"text/template"
)
func main() {
a, err := awsipranges.New(http.DefaultClient)
if err != nil {
fmt.Println(err)
}
cidrs, err := a.PrefixesByService("EC2")
tmpl := template.Must(template.ParseFiles("hoge.tmpl"))
var doc bytes.Buffer
if err := tmpl.Execute(&doc, cidrs); err != nil {
panic(err)
}
fmt.Println(doc.String())
}
AWSTemplateFormatVersion: "2010-09-09"
Description: "auto create by golang"
Parameters: {}
Mappings: {}
Resources:
InstanceSecurityGroupIngress:
Type: "AWS::EC2::SecurityGroup"
Properties:
VpcId: 自環境の既存のVPCのID
GroupDescription: "Enable http access from CloudFront"
SecurityGroupIngress:
{{range .}} -
IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: {{.}}
{{end}}
Outputs:
InstanceSecurityGroupIngress:
Value: !Ref InstanceSecurityGroupIngress
Export:
Name: InstanceSecurityGroupIngress
Copyright (c) 2017 dekokun
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment