Aws

ApiGateway Post 에 특정한 문자 들어있으면 오류 발생시키기

wngnl05 2024. 12. 27. 15:00

통합요청 매핑 템플릿 코드

- id에 "admin"이 포함되어 있지 않으면 DynamoDB에 저장

더보기
#set($inputRoot = $input.path('$'))
#if(!$input.params('id').contains('admin'))
  {
    "TableName": "serverless-user-table",
    "Item": {
        "id": {
            "S": "$input.params('id')"
        },
        "age": {
            "N": "$input.params('age')"
        },
        "company": {
            "S": "$input.params('company')"
        }
    }
  }
#end

통합응답 매핑 템플릿 코드

- id에 "admin"이 포함되어 있지 않으면 200 반환 포함되어 있으면 500 반환

더보기
#if(!$input.params('id').contains('admin'))
  #set($context.responseOverride.status = 200)
  {"msg": "Success insert data"}
#else
  #set($context.responseOverride.status = 500)
  {"message": "Internal server error"}
#end
더보기

요청

{ 
  "TableName": "serverless-user-table",
  "Key": {
    "id": { 
      "S": "$input.params('id')"
    }
  }
}

응답

#set($inputRoot = $input.path('$'))
{
  "id": "$inputRoot.Item.id.S",
  "age": "$inputRoot.Item.age.N"
  "company": "$inputRoot.Item.company.S"
}

[]

{msg: Success insert data} -status 200

status 500

{id:wngnl, age:19, company:hrdkorea} -status 200

DynamoDB intergation found

확인 후 Dynamo DB 데이터 삭제하기

aws dynamodb scan --table-name serverless-user-table --query Items
echo "-------------"
apiId=$(aws apigateway get-rest-apis --query items[].id --output text)
name="wngnl"
curl -sS -w "\n - status code: %{http_code} \n" -X POST "https://$apiId.execute-api.ap-northeast-2.amazonaws.com/v1/user?id=$name&age=19&company=hrdkorea"
echo "-------------"
apiId=$(aws apigateway get-rest-apis --query items[].id --output text)
curl -sS -w "\n - status code: %{http_code} \n" -o /dev/null -X POST "https://$apiId.execute-api.ap-northeast-2.amazonaws.com/v1/user?id=$name-admin&age=19&company=hrdkorea"
echo "-------------"
apiId=$(aws apigateway get-rest-apis --query items[].id --output text)
curl -sS -w "\n - status code: %{http_code} \n" -X GET "https://$apiId.execute-api.ap-northeast-2.amazonaws.com/v1/user?id=$name"
echo "-------------"
apiId=$(aws apigateway get-rest-apis --query "items[?name=='serverless-api-gw'].id" --output text) && resourceId=$(aws apigateway get-resources --rest-api-id $apiId --query "items[?path=='/user'].id" --output text) && aws apigateway get-integration --rest-api-id $apiId --resource-id $resourceId --http-method GET --query "uri" --output text | grep -q 'dynamodb' && echo "DynamoDB integration found" || echo "DynamoDB integration not found"
echo "-------------"
aws dynamodb scan --table-name serverless-user-table --query Items