ããã«ã¡ã¯ããã£ããããã¡ã³ããµã¼ãã¹ï¼èª²ã®å±±æ¬ã§ãã
æããªã£ã¦ãã¦ããµã¦ããæ°æã¡ããå£ç¯ã«ãªã£ã¦ãã¾ããã
æ¬æ¥ã¯ AWS Serverless Application Model (以éã¯SAM) ã使ã£ã¦ãAPI Gateway ã« IPã¢ãã¬ã¹å¶éããããæ¹æ³ããç´¹ä»ãã¾ãã éçºä¸ã®APIããã¹ãããããã©ãèªè¨¼æ©è½ã®å®è£ ã¯æªå®ããã«ãªã¼ãã³ã«ãããããªãã¨ããæ¹ã¯å¿ è¦ã§ãã
- ãã®è¨äºã®å¯¾è±¡è ã¯ï¼
- é²ãæ¹
- SAM ãã³ãã¬ã¼ã
- ãªã½ã¼ã¹ããªã·ã¼
- IP ã¢ãã¬ã¹å¶éã®ç¢ºèª
- ãã¾ãï¼IpRangeWhitelistï¼
- SAM ãã³ãã¬ã¼ãï¼IpRangeWhitelist ãå©ç¨ï¼
- ãªã½ã¼ã¹ããªã·ã¼ï¼IpRangeWhitelist ãå©ç¨ï¼
- ã¾ã¨ã
- ãããã«
ãã®è¨äºã®å¯¾è±¡è ã¯ï¼
ãã®è¨äºã¯ä»¥ä¸ã®æ¹ã ã«åãã¦æ¸ããã¦ãã¾ã
- API Gateway ã« IP ã¢ãã¬ã¹å¶éããããã
- SAM ã使ã£ã¦ç®¡çããã
é²ãæ¹
以ä¸ã®AWSå ¬å¼ããã¥ã¡ã³ããåèã«ãã¾ãã repost.aws
AWS::Serverless::Api -> ApiAuth -> ResourcePolicy -> CustomStatements
ã«IPã¢ãã¬ã¹å¶éç¨ã®ãªã½ã¼ã¹ããªã·ã¼ã追è¨ãã¦ããã¾ãã
å¾åã«SAMç¹æã®ç°¡åãªè¨å®æ¹æ³ãè¨è¼ãããã¾ãã®ã§ãæå¾ã¾ã§ã覧é ããã°ã¨æãã¾ãã
â» ãªã½ã¼ã¹ããªã·ã¼ã®è¨å®ã¯ REST API ã®ã¿ã®ãããHTTP API ã¯é対å¿ã¨ãªãã¾ãã
SAM ãã³ãã¬ã¼ã
sam init
ã³ãã³ãã§åå¾ã§ããHello World ã®ãã³ãã¬ã¼ãããã¼ã¹ã«ãã¦
Globals ã®è¨å®ã« ãªã½ã¼ã¹ããªã·ã¼ã®å
容ã追è¨ãã¾ãã
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: sam-app Globals: Function: Timeout: 3 MemorySize: 128 # IPã¢ãã¬ã¹å¶éã追å â Api: Auth: ResourcePolicy: CustomStatements: [ { "Effect": "Allow", "Principal": "*", "Action": "execute-api:Invoke", "Resource": "execute-api:/*", }, { "Effect": "Deny", "Principal": "*", "Action": "execute-api:Invoke", "Resource": "execute-api:/*", "Condition": { "NotIpAddress": { "aws:SourceIp": [ "{許å¯ãããIPã¢ãã¬ã¹}", ], }, }, }, ] # IPã¢ãã¬ã¹å¶éã追å â Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.9 Architectures: - x86_64 Events: HelloWorld: Type: Api Properties: Path: /hello Method: get
ãªã½ã¼ã¹ããªã·ã¼
IP ã¢ãã¬ã¹å¶éã®ç¢ºèª
許å¯ãããIP ã¢ãã¬ã¹ããã®ã¢ã¯ã»ã¹
{ "message": "hello world" }
ç¦æ¢ããã IP ã¢ãã¬ã¹ããã®ã¢ã¯ã»ã¹
{ "Message": "User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:ap-northeast-1:************:endqsfr6uk/Prod/GET/hello with an explicit deny" }
ç¡äºãIP ã¢ãã¬ã¹å¶éãããããã¨ãã§ãã¾ããã
ãã¾ãï¼IpRangeWhitelistï¼
AWS::Serverless::Api -> ApiAuth -> ResourcePolicy
ã®å
容ã調ã¹ãæä¸ãããã£ã¨æãé
ç®ãè¦ã¤ãã¾ããã
IpRangeWhitelist
許å¯ãã IP ã¢ãã¬ã¹ã¾ãã¯ã¢ãã¬ã¹ç¯å²ã§ãã
ã¿ã¤ã: ãªã¹ã
å¿ é : ããã
AWS CloudFormation ã¨ã®äºææ§: ãã®ããããã£ã¯ AWS SAM ã«åºæã§ãããAWS CloudFormation ã«åçã®ãã®ã¯ããã¾ããã
ããã使ãã°ããã£ã¨ç°¡åã«æ¸ããã®ã§ã¯ï¼ 試ãã¦ã¿ã¾ãã
SAM ãã³ãã¬ã¼ãï¼IpRangeWhitelist ãå©ç¨ï¼
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: sam-app Globals: Function: Timeout: 3 MemorySize: 128 # IPã¢ãã¬ã¹å¶éã追å â Api: Auth: ResourcePolicy: IpRangeWhitelist: - "{許å¯ãããIPã¢ãã¬ã¹}" # IPã¢ãã¬ã¹å¶éã追å â Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.9 Architectures: - x86_64 Events: HelloWorld: Type: Api Properties: Path: /hello Method: get
ãªã½ã¼ã¹ããªã·ã¼ï¼IpRangeWhitelist ãå©ç¨ï¼
IP ã¢ãã¬ã¹ç¨ã®ãªã½ã¼ã¹ããªã·ã¼ãèªåçæããã¦ã¾ãã
SAM ã§æ§ç¯ããAPIãã¹ã«ãªã½ã¼ã¹ãéå®ããã¦ãããããè¨å®ãå®å
¨ãã¤æ¥½ã«ãªã£ã¦ã¾ãã
ã¾ã¨ã
- SAM ã® CustomStatements ã«ãªã½ã¼ã¹ããªã·ã¼ã®å 容ãè¨è¼ãããã¨ã§ IP ã¢ãã¬ã¹å¶éã¯å¯è½
- SAM ã® IpRangeWhitelist ãå©ç¨ãããã¨ã§ãIP ã¢ãã¬ã¹ãè¨è¼ããã ãã§ãªã½ã¼ã¹ããªã·ã¼ãèªåçæããã
- å®å ¨ãã¤æ¥½ãª IpRangeWhitelist ãå©ç¨ãã¾ããã
ãããã«
常ã«ç°¡åãªæ¹æ³ã模索ãã¦ããã¾ãï¼
å±±æ¬ ç大(å·çè¨äºã®ä¸è¦§)
ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¹é¨ ãã£ããããã¡ã³ããµã¼ãã¹1課
2023å¹´8æå ¥ç¤¾ãã«ããã©ããã好ãã