LoginSignup
2
2

More than 5 years have passed since last update.

CloudFormationを使ってELBのアクセスログ用のS3バケットを作成する

Posted at

はじめに

本記事では、AWS CloudFormationを使って、ELBのアクセスログ用のS3バケットを作成する手順を説明しています。(初心者向け)

本記事で掲載しているテンプレートの最新版は、下記に置いてます。
https://github.com/okubo-t/aws-cloudformation

CloudFormationを使ってELB(ALB)を構築する手順については、下記を参照してください。
CloudFormationを使ってELBとEC2(1台)を構築する
CloudFormationを使ってELBとEC2(2台)を構築する
CloudFormationを使ってALBとEC2(1台)を構築する
CloudFormationを使ってALBとEC2(2台)を構築する

設定手順

1 AWS CloudFormation管理コンソールから、スタックの作成をクリックします。

2 後述のテンプレートを選択します。

3 各パラメータを入力します。

パラメータ名 用途 備考
スタックの名前 テンプレートから作成するリソース一式の名前 例 prd-stack-vpc-20180801
ELBLogBucketName ELBのアクセスログを出力するS3バケットの名前

4 後続は、デフォルトのまま次へ次へで、作成します。

5 状況が CREATE COMPLETEになれば、ELBのアクセスログ用のS3バケットの作成が完了です。

6 EC2の管理コンソールで、ELB(ALB)のアクセスログに、CloudFormationで作成したS3バケットを設定します。

テンプレート

ELBを構築するCloudFromationのテンプレートに組み込んでも良いかと思います。

s3-elb-accesslog.yml
AWSTemplateFormatVersion: "2010-09-09"
Description:
  S3 Bucket for ELB AccessLog Create

Metadata:
  "AWS::CloudFormation::Interface":
    ParameterGroups:
      - Label: 
          default: "S3 Bucket for ELB AccessLog Configuration"
        Parameters: 
          - ELBLogBucketName

    ParameterLabels: 
      ELBLogBucketName: 
        default: "ELBLogBucketName"

# ------------------------------------------------------------#
# Input Parameters
# ------------------------------------------------------------# 
Parameters:
  ELBLogBucketName:
    Type: String

# ------------------------------------------------------------#
# ELBAccountId Mappings
# ------------------------------------------------------------# 
Mappings:
  ELBAccountId:
    us-east-1:
      "AccountId": "127311923021"
    us-west-2:
      "AccountId": "797873946194"
    us-west-1:
      "AccountId": "027434742980"
    eu-west-1:
      "AccountId": "156460612806"
    ap-southeast-1:
      "AccountId": "114774131450"
    ap-southeast-2:
      "AccountId": "783225319266"
    ap-northeast-1:
      "AccountId": "582318560864"
    sa-east-1:
      "AccountId": "507241528517"
    us-gov-west-1:
      "AccountId": "048591011584"

Resources:
# ------------------------------------------------------------#
#  S3
# ------------------------------------------------------------#        
# ELBLogBucket
  ELBLogBucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: !Ref ELBLogBucketName

#BucketPolicy
  ELBLogBucketPolicy:
    Type: "AWS::S3::BucketPolicy"
    Properties:
      Bucket: !Ref ELBLogBucket
      PolicyDocument:
        Id: "AWSCFn-AccessLogs-Policy-20180920"
        Version: "2012-10-17"
        Statement:
          - Sid: AWSCFn-20180920
            Effect: "Allow"
            Action:
              - "s3:PutObject"
            Resource: !Sub "arn:aws:s3:::${ELBLogBucket}/AWSLogs/${AWS::AccountId}/*"
            Principal:
              AWS: !FindInMap [ ELBAccountId, !Ref "AWS::Region", AccountId ]

# ------------------------------------------------------------#                
# Output Parameters
# ------------------------------------------------------------#                
Outputs:
#ELBLogBucket
  ELBLogBucket:
    Value: !Ref ELBLogBucket
    Export:
      Name: !Ref ELBLogBucketName
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2