簡體   English   中英

Cloudformation 無法為 apigateway 創建資源策略

[英]Cloudformation unable to create resource policy for apigateway

當我直接將資源策略傳遞給控制台時,它工作正常。 以下是資源策略示例:-

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:us-west-2:339159142535:ooxmwl6q4e/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        ""14.98.8.190/32""
                    ]
                }
            }
        }
    ]
}

現在如何為此創建一個 cloudformation 模板以創建並附加到 apigateway

我試圖創建一個政策,但根據新政策“校長”被貶低了。

我也創建了一個角色,但沒有幫助。 以下是角色片段:-

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "Apifirewall": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "apigateway.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Policies": [
                    {
                        "PolicyName": "Apifirewall",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": "*",
                                    "Resource": [
                                        "arn:aws:execute-api:us-west-2:339159142535:ooxmwl6q4e/*"
                                    ],
                                    "Condition": {
                                        "IpAddress": {
                                            "aws:SourceIp": [
                                                "14.98.8.190/32"
                                            ]
                                        }
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    },
    "Outputs": {
        "Apifirewall": {
            "Value": {
                "Fn::GetAtt": [
                    "Apifirewall",
                    "Arn"
                ]
            }
        }
    }
}

APIGateway 資源策略不綁定到 IAM 策略,它是不同類型的資源。

因此,要在您的 RestApi 上實現它,您應該在AWS::ApiGateway::RestApi資源上使用Policy參數

{
  "Type" : "AWS::ApiGateway::RestApi",
  "Properties" : {  
    "ApiKeySourceType" : String,
    "BinaryMediaTypes" : [ String, ... ],
    "Body" : JSON object,
    "BodyS3Location" : S3Location,
    "CloneFrom" : String,
    "Description" : String,      
    "EndpointConfiguration" : EndpointConfiguration,
    "FailOnWarnings" : Boolean,
    "MinimumCompressionSize" : Integer,
    "Name" : String,
    "Parameters" : { String:String, ... },
    "Policy" : JSON object
  }
}

下面是使用 lambda 集成的 api 部署的整個 CFT

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "AppEnv": {
            "Type": "String",
            "Description": "Application environment, for this deployment"
        },
        "DeployTag": {
            "Type": "String",
            "Description": "Distinct deployment tag ex: BLUE, GREEN"
        }
    },
    "Resources": {
        "LambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/AWSLambdaFullAccess"
                ]
            }
        },
        "RecommenderLambda": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "recommender_field_validation_lambda.lambda_handler",
                "FunctionName": "recommenderlambda2",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaExecutionRole",
                        "Arn"
                    ]
                },
                "Environment": {
                    "Variables": {
                        "S3_BUCKET": "belcorp.recommender.test",
                        "REGION_NAME": "us-west-2",
                        "TOPIC_ARN": {
                            "Fn::ImportValue": "RecommenderTopicARN"
                        },
                        "TABLE_NAME": {
                            "Fn::ImportValue": "recommederrequestinfo"
                        }
                    }
                },
                "Code": {
                    "S3Bucket": "belcorp.recommender.lambdas",
                    "S3Key": "recommender_field_validation_lambda.zip"
                },
                "Runtime": "python3.6",
                "Timeout": 25
            }
        },
        "LambdaPermission": {
            "DependsOn": "RecommenderLambda",
            "Type": "AWS::Lambda::Permission",
            "Properties": {
                "Action": "lambda:invokeFunction",
                "FunctionName": "recommenderlambda2",
                "Principal": "apigateway.amazonaws.com",
                "SourceArn": {
                    "Fn::Join": [
                        "",
                        [
                            "arn:aws:execute-api:",
                            {
                                "Ref": "AWS::Region"
                            },
                            ":",
                            {
                                "Ref": "AWS::AccountId"
                            },
                            ":",
                            {
                                "Ref": "RecommenderApi"
                            },
                            "/*"
                        ]
                    ]
                }
            }
        },
        "RecommenderApi": {
            "Type": "AWS::ApiGateway::RestApi",
            "Properties": {
                "EndpointConfiguration": {
                    "Types": [
                        "EDGE"
                    ]
                },
                "Description": "RecommenderAPI",
                "Name": {
                    "Fn::Sub": "RecommenderApi-${AppEnv}-${DeployTag}"
                },
                "Policy": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": "*",
                            "Action": "execute-api:Invoke",
                            "Resource": {
                                "Fn::Sub": "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*"
                            },
                            "Condition": {
                                "IpAddress": {
                                    "aws:SourceIp": [
                                        "14.98.8.190/32"
                                    ]
                                }
                            }
                        }
                    ]
                }
            }
        },
        "ApiGatewayAccount": {
            "Type": "AWS::ApiGateway::Account",
            "Properties": {
                "CloudWatchRoleArn": {
                    "Fn::ImportValue": "cloudwatchRole"
                }
            }
        },
        "ApiDeployment": {
            "Type": "AWS::ApiGateway::Deployment",
            "DependsOn": [
                "OfferPostMethod",
                "OrderPostMethod"
            ],
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "StageName": "dev"
            }
        },
        "ProcessInput": {
            "Type": "AWS::ApiGateway::Resource",
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "ParentId": {
                    "Fn::GetAtt": [
                        "RecommenderApi",
                        "RootResourceId"
                    ]
                },
                "PathPart": "process-input"
            }
        },
        "OfferLevel": {
            "Type": "AWS::ApiGateway::Resource",
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "ParentId": {
                    "Ref": "ProcessInput"
                },
                "PathPart": "offer-level"
            }
        },
        "OrderLevel": {
            "Type": "AWS::ApiGateway::Resource",
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "ParentId": {
                    "Ref": "ProcessInput"
                },
                "PathPart": "order-level"
            }
        },
        "OfferPostMethod": {
            "DependsOn": "RecommenderLambda",
            "Type": "AWS::ApiGateway::Method",
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "ResourceId": {
                    "Ref": "OfferLevel"
                },
                "HttpMethod": "POST",
                "AuthorizationType": "NONE",
                "Integration": {
                    "Type": "AWS_PROXY",
                    "IntegrationHttpMethod": "POST",
                    "Uri": {
                        "Fn::Join": [
                            "",
                            [
                                "arn:aws:apigateway:",
                                {
                                    "Ref": "AWS::Region"
                                },
                                ":lambda:path/2015-03-31/functions/",
                                {
                                    "Fn::GetAtt": [
                                        "RecommenderLambda",
                                        "Arn"
                                    ]
                                },
                                "/invocations"
                            ]
                        ]
                    },
                    "IntegrationResponses": [
                        {
                            "StatusCode": 200,
                            "ResponseTemplates": {
                                "application/json": "$input.json('$.body')"
                            }
                        }
                    ]
                }
            }
        },
        "OrderPostMethod": {
            "DependsOn": "RecommenderLambda",
            "Type": "AWS::ApiGateway::Method",
            "Properties": {
                "RestApiId": {
                    "Ref": "RecommenderApi"
                },
                "ResourceId": {
                    "Ref": "OrderLevel"
                },
                "HttpMethod": "POST",
                "AuthorizationType": "NONE",
                "Integration": {
                    "Type": "AWS_PROXY",
                    "IntegrationHttpMethod": "POST",
                    "Uri": {
                        "Fn::Join": [
                            "",
                            [
                                "arn:aws:apigateway:",
                                {
                                    "Ref": "AWS::Region"
                                },
                                ":lambda:path/2015-03-31/functions/",
                                {
                                    "Fn::GetAtt": [
                                        "RecommenderLambda",
                                        "Arn"
                                    ]
                                },
                                "/invocations"
                            ]
                        ]
                    },
                    "IntegrationResponses": [
                        {
                            "StatusCode": 200,
                            "ResponseTemplates": {
                                "application/json": "$input.json('$.body')"
                            }
                        }
                    ]
                }
            }
        }
    },
    "Outputs": {
        "RootUrl": {
            "Description": "Root URL of the API gateway",
            "Value": {
                "Fn::Join": [
                    "",
                    [
                        "https://",
                        {
                            "Ref": "RecommenderApi"
                        },
                        ".execute-api.",
                        {
                            "Ref": "AWS::Region"
                        },
                        ".amazonaws.com"
                    ]
                ]
            }
        },
        "OfferUrl": {
            "Description": "Root URL of the API gateway",
            "Value": {
                "Fn::Join": [
                    "",
                    [
                        "https://",
                        {
                            "Ref": "RecommenderApi"
                        },
                        ".execute-api.",
                        {
                            "Ref": "AWS::Region"
                        },
                        ".amazonaws.com",
                        "/dev/process-input/offer-level"
                    ]
                ]
            }
        },
        "OrderUrl": {
            "Description": "Root URL of the API gateway",
            "Value": {
                "Fn::Join": [
                    "",
                    [
                        "https://",
                        {
                            "Ref": "RecommenderApi"
                        },
                        ".execute-api.",
                        {
                            "Ref": "AWS::Region"
                        },
                        ".amazonaws.com",
                        "/dev/process-input/order-level"
                    ]
                ]
            }
        }
    }
}

如果您將 YAML 用於 CloudFormation,則Policy可以采用 YAML。 無需為此使用 JSON。 例如:

  Parameters:
    ApiAllowedIps:
      Type: CommaDelimitedList

  RestApi:
    Type: AWS::ApiGateway::RestApi
    Properties:
      ...
      Policy: 
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action: '*'
            Principal: '*'
            Resource: '*'
            Condition:
              IpAddress:
                aws:SourceIp: !Ref ApiAllowedIps  

評論太長了。 這是從這個答案轉換后的 YAML,評論者指出可以在 CloudFormation Designer 中完成:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  AppEnv:
    Type: String
    Description: 'Application environment, for this deployment'
  DeployTag:
    Type: String
    Description: 'Distinct deployment tag ex: BLUE, GREEN'
Resources:
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/AWSLambdaFullAccess'
  RecommenderLambda:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: recommender_field_validation_lambda.lambda_handler
      FunctionName: recommenderlambda2
      Role: !GetAtt 
        - LambdaExecutionRole
        - Arn
      Environment:
        Variables:
          S3_BUCKET: belcorp.recommender.test
          REGION_NAME: us-west-2
          TOPIC_ARN: !ImportValue RecommenderTopicARN
          TABLE_NAME: !ImportValue recommederrequestinfo
      Code:
        S3Bucket: belcorp.recommender.lambdas
        S3Key: recommender_field_validation_lambda.zip
      Runtime: python3.6
      Timeout: 25
  LambdaPermission:
    DependsOn: RecommenderLambda
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:invokeFunction'
      FunctionName: recommenderlambda2
      Principal: apigateway.amazonaws.com
      SourceArn: !Join 
        - ''
        - - 'arn:aws:execute-api:'
          - !Ref 'AWS::Region'
          - ':'
          - !Ref 'AWS::AccountId'
          - ':'
          - !Ref RecommenderApi
          - /*
  RecommenderApi:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      EndpointConfiguration:
        Types:
          - EDGE
      Description: RecommenderAPI
      Name: !Sub 'RecommenderApi-${AppEnv}-${DeployTag}'
      Policy:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal: '*'
            Action: 'execute-api:Invoke'
            Resource: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*'
            Condition:
              IpAddress:
                'aws:SourceIp':
                  - 14.98.8.190/32
  ApiGatewayAccount:
    Type: 'AWS::ApiGateway::Account'
    Properties:
      CloudWatchRoleArn: !ImportValue cloudwatchRole
  ApiDeployment:
    Type: 'AWS::ApiGateway::Deployment'
    DependsOn:
      - OfferPostMethod
      - OrderPostMethod
    Properties:
      RestApiId: !Ref RecommenderApi
      StageName: dev
  ProcessInput:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      RestApiId: !Ref RecommenderApi
      ParentId: !GetAtt 
        - RecommenderApi
        - RootResourceId
      PathPart: process-input
  OfferLevel:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      RestApiId: !Ref RecommenderApi
      ParentId: !Ref ProcessInput
      PathPart: offer-level
  OrderLevel:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      RestApiId: !Ref RecommenderApi
      ParentId: !Ref ProcessInput
      PathPart: order-level
  OfferPostMethod:
    DependsOn: RecommenderLambda
    Type: 'AWS::ApiGateway::Method'
    Properties:
      RestApiId: !Ref RecommenderApi
      ResourceId: !Ref OfferLevel
      HttpMethod: POST
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Join 
          - ''
          - - 'arn:aws:apigateway:'
            - !Ref 'AWS::Region'
            - ':lambda:path/2015-03-31/functions/'
            - !GetAtt 
              - RecommenderLambda
              - Arn
            - /invocations
        IntegrationResponses:
          - StatusCode: 200
            ResponseTemplates:
              application/json: $input.json('$.body')
  OrderPostMethod:
    DependsOn: RecommenderLambda
    Type: 'AWS::ApiGateway::Method'
    Properties:
      RestApiId: !Ref RecommenderApi
      ResourceId: !Ref OrderLevel
      HttpMethod: POST
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Join 
          - ''
          - - 'arn:aws:apigateway:'
            - !Ref 'AWS::Region'
            - ':lambda:path/2015-03-31/functions/'
            - !GetAtt 
              - RecommenderLambda
              - Arn
            - /invocations
        IntegrationResponses:
          - StatusCode: 200
            ResponseTemplates:
              application/json: $input.json('$.body')
Outputs:
  RootUrl:
    Description: Root URL of the API gateway
    Value: !Join 
      - ''
      - - 'https://'
        - !Ref RecommenderApi
        - .execute-api.
        - !Ref 'AWS::Region'
        - .amazonaws.com
  OfferUrl:
    Description: Root URL of the API gateway
    Value: !Join 
      - ''
      - - 'https://'
        - !Ref RecommenderApi
        - .execute-api.
        - !Ref 'AWS::Region'
        - .amazonaws.com
        - /dev/process-input/offer-level
  OrderUrl:
    Description: Root URL of the API gateway
    Value: !Join 
      - ''
      - - 'https://'
        - !Ref RecommenderApi
        - .execute-api.
        - !Ref 'AWS::Region'
        - .amazonaws.com
        - /dev/process-input/order-level

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM