質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

1806閲覧

aws s3 yaml ベーシック認証をやめてip制限のみに変更する方法

cheche0830

総合スコア187

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2019/03/13 07:12

AWSTemplateFormatVersion: '2010-09-09' Description: Static contents distribution using S3 and CloudFront with basic authentication by lambda@Edge Parameters: AuthUser: Description: ID for basic authentication Type: String Default: user AuthPass: Description: Password for basic authentication Type: String Default: password CloudFrontAliase: Description: CloudFront Alternate Domain Names (CNAMEs) Type: String Default: .test.com SelectLambdaDeployment: Description: Select blue / green deployment Type: String Default: blue AllowedValues: - blue - green Conditions: CloudFrontAliaseEnable: !Not [!Equals [!Ref 'CloudFrontAliase', 'none']] LambdaVersionIsBlue: !Equals [!Ref 'SelectLambdaDeployment', 'blue'] LambdaVersionIsGreen: !Equals [!Ref 'SelectLambdaDeployment', 'green'] Resources: S3Bucket: Type: AWS::S3::Bucket DeletionPolicy: Retain Properties: BucketName: !Sub '${AWS::StackName}.test.com' AccessControl: PublicRead WebsiteConfiguration: IndexDocument: index.html ErrorDocument: error.html VersioningConfiguration: Status: Enabled S3BucketPolicy: Type: AWS::S3::BucketPolicy Properties: Bucket: !Ref 'S3Bucket' PolicyDocument: Statement: - Action: s3:GetObject Effect: Allow Resource: !Sub 'arn:aws:s3:::${S3Bucket}/*' Principal: AWS: !Sub 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginAccessIdentity}' S3BucketCloudFrontLog: Type: AWS::S3::Bucket DeletionPolicy: Retain Properties: BucketName: !Sub 'cloudfrontlog-${AWS::StackName}.test.com' LifecycleConfiguration: Rules: - Id: AutoDelete Status: Enabled ExpirationInDays: 15 CloudFrontDistribution: Type: AWS::CloudFront::Distribution Properties: DistributionConfig: Origins: - Id: S3Origin DomainName: !GetAtt 'S3Bucket.DomainName' S3OriginConfig: OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}' Enabled: true DefaultRootObject: index.html Comment: !Sub '${AWS::StackName} distribution' Logging: IncludeCookies: 'false' Bucket: !GetAtt 'S3BucketCloudFrontLog.DomainName' Prefix: !Sub '${AWS::StackName}/${SelectLambdaDeployment}' Aliases: - !If - CloudFrontAliaseEnable - !Ref 'CloudFrontAliase' - !Ref 'AWS::NoValue' DefaultCacheBehavior: TargetOriginId: S3Origin ForwardedValues: QueryString: true Headers: - If-Modified-Since - If-None-Match - Upgrade-Insecure-Requests - User-Agent Cookies: Forward: all ViewerProtocolPolicy: allow-all DefaultTTL: '0' MaxTTL: '0' MinTTL: '0' LambdaFunctionAssociations: - EventType: viewer-request LambdaFunctionARN: !If - LambdaVersionIsBlue - !Ref 'LambdaFunctionVersionBlue' - !Ref 'LambdaFunctionVersionGreen' CacheBehaviors: - AllowedMethods: - GET - HEAD TargetOriginId: S3Origin ForwardedValues: QueryString: false PathPattern: '/favicon.ico' ViewerProtocolPolicy: allow-all DefaultTTL: '86400' MaxTTL: '86400' MinTTL: '86400' CloudFrontOriginAccessIdentity: Type: AWS::CloudFront::CloudFrontOriginAccessIdentity Properties: CloudFrontOriginAccessIdentityConfig: Comment: !Ref 'AWS::StackName' Route53DNSRecord: Type: AWS::Route53::RecordSet Properties: HostedZoneName: test.com . Name: !Sub '${AWS::StackName}.test.com .' Type: A AliasTarget: HostedZoneId: Z2FDTNDATAQYW2 DNSName: !GetAtt 'CloudFrontDistribution.DomainName' LogGroupLambda: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub '/aws/lambda/${LambdaFunction}' RetentionInDays: 7 LambdaRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com - edgelambda.amazonaws.com Action: - sts:AssumeRole Path: /service-role/ Policies: - PolicyName: root PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:PutLogEvents Resource: arn:aws:logs:*:*:* LambdaFunction: Type: AWS::Lambda::Function Properties: Handler: index.handler Role: !GetAtt 'LambdaRole.Arn' Code: ZipFile: !Sub | 'use strict'; exports.handler = (event, context, callback) => { // Get request and request headers const request = event.Records[0].cf.request; const headers = request.headers; // Configure authentication const authUser = '${AuthUser}'; const authPass = '${AuthPass}'; // Construct the Basic Auth string const authString = 'Basic ' + new Buffer(authUser + ':' + authPass).toString('base64'); // Require Basic authentication if (typeof headers.authorization == 'undefined' || headers.authorization[0].value != authString) { const body = 'Unauthorized'; const response = { status: '401', statusDescription: 'Unauthorized', body: body, headers: { 'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}] }, }; // Debug log console.log("request: " + JSON.stringify(request)); callback(null, response); } // Instead of index document processing var olduri = request.uri; var newuri = olduri.replace(//$/, '/index.html'); if ( olduri != newuri ) { console.log("Old URI: " + olduri); console.log("New URI: " + newuri); } request.uri = newuri; // Continue request processing if authentication passed callback(null, request); }; Runtime: nodejs6.10 MemorySize: 128 Timeout: 1 Description: Basic authentication with Lambda@Edge Tags: - Key: CloudformationArn Value: !Ref 'AWS::StackId' LambdaFunctionVersionBlue: Type: AWS::Lambda::Version Condition: LambdaVersionIsBlue Properties: FunctionName: !Ref 'LambdaFunction' LambdaFunctionVersionGreen: Type: AWS::Lambda::Version Condition: LambdaVersionIsGreen Properties: FunctionName: !Ref 'LambdaFunction' IamGroup: Type: AWS::IAM::Group Properties: GroupName: !Sub 'iam-group-s3-access-${S3Bucket}' Policies: - PolicyName: PolicieAllow PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:List* - s3:GetBucketLocation Resource: - arn:aws:s3:::* - Effect: Allow Action: - s3:* Resource: - !Sub 'arn:aws:s3:::${S3Bucket}/*' - Effect: Deny Action: - s3:PutBucket* - s3:PutObjectAcl - s3:PutObjectVersionAcl Resource: - arn:aws:s3:::* IamUser: Type: AWS::IAM::User Properties: UserName: !Sub 'iam-user-s3-access-${S3Bucket}' Groups: - !Ref 'IamGroup' Outputs: URL: Value: !Sub 'http://${CloudFrontDistribution.DomainName}' IamUser: Value: !Ref 'IamUser'

aws s3にて
CloudFront:Basic認証用のフィルターとして
lambda:CloudFrontへのアクセスをトリガーにしてBasic認証を実装
route53:サブドメインの設定
cloudFormation:上記のサービスをyamlファイルで一括設定

という設定状態なのですが、こちらのbasic認証をやめて
ip制限のみにしたいのですが、どのように変更すればよいでしょうか。。
投げやりで申し訳ありませんが、まずどこをどう見てよいのすらわからず・・
(制作が前任者で引継ぎもなく・・・)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sujico.net

2019/03/13 07:16

IP制限を行いたいのはS3バケットに対してでしょうか?
cheche0830

2019/03/13 07:20

ご質問ありがとうございます。 すいません、、S3バケットなるものがよくわかっていなくて・・・ ただ前任者がS3はただの置き場でbasic認証などはCloudFrontをかます必要があるといっていたので、CloudFrontの方にIP制限をかけるのかな?という認識でいました。。すいません、的外れな回答でしたら・・・ 実現したいことはs3においてあるhttp://test.com/にアクセスした場合、 許可されたipからしかwebアクセスできないようにすることが目標です!
guest

回答1

0

ベストアンサー

S3バケットに対してIPアドレス制限を行う場合は、バケットポリシーを使用するのが有効かと思います。
S3バケットでホストされている静的サイトの制限も行う事が出来るかと存じます。

例えば以下のようにすると、
IPアドレスxxx.xxx.xxx.xxx と yyy.yyy.yyy.yyy に対し
バケットのフルアクセス権限が与えられます。

{ "Version": "2012-10-17", "Id": "S3PolicyId1", "Statement": [ { "Sid": "IPAllow", "Effect": "Allow", "Principal": "*", "Action": "s3:*", "Resource": "arn:aws:s3:::【バケット名】/*", "Condition": { "IpAddress": { "aws:SourceIp": [ "xxx.xxx.xxx.xxx", "yyy.yyy.yyy.yyy" ] } } } ] }

詳細な仕様は以下 AWS公式ドキュメントをご確認下さい。
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/example-bucket-policies.html

また、AWSのビジネスサポートプランに加入すれば
このような技術サポートを受けることが可能です。
https://aws.amazon.com/jp/premiumsupport/business-support/

既存のCloudFront, Lambdaなどの資産に関しましては、
何とも申し上げることが出来かねます。

恐れ入りますが、上記で要件が満たせるかご確認頂けますと幸いです。

投稿2019/03/13 07:22

編集2019/03/13 07:26
sujico.net

総合スコア453

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cheche0830

2019/03/13 07:46

ありがとうございます!調査の取っ掛かりが出来ましたので調べてみます! 取り急ぎは、S3BucketPolicy:の部分にいただいたものを付け加えてみたいと思います!
sujico.net

2019/03/13 08:04

ご確認いただきありがとうございます。 宜しくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問