Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Created March 11, 2014 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mechamogera/9481458 to your computer and use it in GitHub Desktop.
Save mechamogera/9481458 to your computer and use it in GitHub Desktop.
S3のstaticsホスティングをする構成のCloudFormationのTemplate
#!/bin/env ruby
require 'aws-sdk'
target = File.read(ARGV[0])
cf = AWS::CloudFormation.new(proxy_uri: ENV['HTTP_PROXY'] || ENV['http_proxy'])
stack = cf.stacks.create('test', target)
while stack.status == "CREATE_IN_PROGRESS"
sleep 10
end
puts stack.status
puts stack.status_reason
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem "aws-sdk"
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Sample",
"Parameters" : {
"StaticsDomain" : {
"Type" : "String",
"Default" : "hogemoge.test.com",
"Description" : "domain for statics"
},
"StaticsHostedZone" : {
"Type" : "String",
"Default" : "test.com",
"Description" : "hosted zone for statics"
}
},
"Resources" : {
"StaticsBucket" : {
"Type" : "AWS::S3::Bucket",
"Properties" : {
"BucketName" : { "Ref" : "StaticsDomain" },
"WebsiteConfiguration" : { "IndexDocument" : "index.html", "ErrorDocument" : "error.html" }
}
},
"StaticsBucketPolicy" : {
"Type" : "AWS::S3::BucketPolicy",
"Properties" : {
"Bucket" : { "Ref" : "StaticsBucket" },
"PolicyDocument" : {
"Statement": [{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": ["s3:GetObject"],
"Resource": { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "StaticsBucket" } , "/*" ]] },
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"192.168.0.1/32"
]
}
}
}]
}
}
},
"StaticsRecord" : {
"Type" : "AWS::Route53::RecordSet",
"Properties" : {
"HostedZoneName" : { "Fn::Join" : ["", [{ "Ref" : "StaticsHostedZone" }, "."]] },
"Name" : { "Fn::Join" : ["", [{ "Ref" : "StaticsDomain" }, "."]] },
"Type" : "CNAME",
"Comment" : "TestCreated",
"TTL" : "300",
"ResourceRecords" : [
{ "Fn::Join" : ["", [{ "Ref" : "StaticsBucket" }, ".s3-website-", { "Ref" : "AWS::Region" }, ".amazonaws.com" ]] }
]
}
}
}
}
#!/bin/env ruby
require 'json'
require 'aws-sdk'
target = File.read(ARGV[0])
JSON.parse(target)
cf = AWS::CloudFormation.new(proxy_uri: ENV['HTTP_PROXY'] || ENV['http_proxy'])
puts cf.validate_template(target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment