Created
November 2, 2016 20:50
-
-
Save moduspwnens/13c5422de5db15a0570103272eca4bd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
AWSTemplateFormatVersion: '2010-09-09' | |
Resources: | |
SiteBucket: | |
Type: AWS::S3::Bucket | |
Properties: | |
WebsiteConfiguration: | |
ErrorDocument: error.html | |
IndexDocument: index.html | |
SiteBucketPolicy: | |
Type: AWS::S3::BucketPolicy | |
Properties: | |
Bucket: | |
Ref: SiteBucket | |
PolicyDocument: | |
Statement: | |
- Action: s3:GetObject | |
Effect: Allow | |
Principal: '*' | |
Resource: | |
Fn::Sub: arn:aws:s3:::${SiteBucket}/* | |
- Action: s3:ListBucket | |
Effect: Allow | |
Principal: '*' | |
Resource: | |
Fn::Sub: arn:aws:s3:::${SiteBucket} | |
# | |
# S3 Bucket Web Site Domain Formatter Function | |
# | |
# CloudFormation's Fn::GetAtt for an S3Bucket provides two attributes: | |
# * DomainName - DNS Name of the bucket | |
# * WebsiteURL - URL of the website endpoint for the bucket | |
# | |
# However, we want the domain name of the website endpoint for the bucket. | |
# It needs to be specified directly for API Gateway to properly proxy | |
# requests to it. | |
# | |
# This Lambda function simply strips the domain name from the website URL | |
# and returns it so it can be exported in the stack's outputs. | |
# | |
S3BucketWebSiteDomainFormatterFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Description: Clears out the stack's S3 bucket | |
Handler: index.lambda_handler | |
MemorySize: 128 | |
Role: | |
Fn::GetAtt: | |
- S3BucketWebSiteDomainFormatterFunctionRole | |
- Arn | |
Code: | |
ZipFile: |- | |
"""S3BucketWebSiteDomainFormatterFunction | |
Used as a CloudFormation custom resource to return the domain of a URL. | |
""" | |
from __future__ import print_function | |
import json | |
import urlparse | |
import cfnresponse | |
handler_object = None | |
def lambda_handler(event, context): | |
print("Event: {}".format(json.dumps(event))) | |
request_type = event.get("RequestType") | |
response_data = {} | |
if request_type in ["Create", "Update"]: | |
response_data["WebsiteDomain"] = get_domain_from_url(event["ResourceProperties"]["WebsiteUrl"]) | |
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data, None) | |
return {} | |
def get_domain_from_url(website_url): | |
parsed_url = urlparse.urlparse(website_url) | |
return parsed_url.netloc.split(":")[0] | |
Runtime: python2.7 | |
Timeout: '300' | |
S3BucketWebSiteDomainFormatterFunctionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Action: | |
- sts:AssumeRole | |
Path: "/" | |
S3BucketWebSiteDomainFormatterFunctionRoleActions: | |
Type: AWS::IAM::Policy | |
Properties: | |
PolicyName: S3BucketWebSiteDomainFormatterFunctionRoleActions | |
PolicyDocument: | |
Version: '2012-10-17' | |
Statement: | |
- Effect: Allow | |
Action: | |
- logs:CreateLogStream | |
- logs:PutLogEvents | |
Resource: | |
Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${S3BucketWebSiteDomainFormatterFunction}:log-stream:* | |
Roles: | |
- Ref: S3BucketWebSiteDomainFormatterFunctionRole | |
S3BucketWebSiteDomainFormatterFunctionLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: | |
Fn::Sub: /aws/lambda/${S3BucketWebSiteDomainFormatterFunction} | |
S3BucketWebSiteDomainRetrieval: | |
Type: Custom::S3BucketWebSiteDomainRetrieval | |
Properties: | |
ServiceToken: | |
Fn::GetAtt: | |
- S3BucketWebSiteDomainFormatterFunction | |
- Arn | |
WebsiteUrl: | |
Fn::GetAtt: | |
- SiteBucket | |
- WebsiteURL | |
DependsOn: | |
- S3BucketWebSiteDomainFormatterFunctionLogGroup | |
- S3BucketWebSiteDomainFormatterFunctionRoleActions | |
Outputs: | |
S3BucketWebSiteDomain: | |
Value: | |
Fn::GetAtt: | |
- S3BucketWebSiteDomainRetrieval | |
- WebsiteDomain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment