AWS SDK 또는 PutIntegrationResponse CLI와 함께 사용 - AWS SDK 코드 예제

AWS 문서 AWS SDK 예제 리포지토리에 더 많은 SDK 예제가 있습니다. GitHub

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK 또는 PutIntegrationResponse CLI와 함께 사용

다음 코드 예제는 PutIntegrationResponse의 사용 방법을 보여줍니다.

작업 예제는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

CLI
AWS CLI

정의된 매핑 템플릿을 사용하여 통합 응답을 기본 응답으로 생성하는 방법

명령:

aws apigateway put-integration-response --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET --status-code 200 --selection-pattern "" --response-templates '{"application/json": "{\"json\": \"template\"}"}'

정규식이 400이고 헤더 값이 정적으로 정의된 통합 응답을 생성하는 방법

명령:

aws apigateway put-integration-response --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET --status-code 400 --selection-pattern 400 --response-parameters '{"method.response.header.custom-header": "'"'"'custom-value'"'"'"}'
Python
SDK for Python(Boto3)
참고

자세한 내용은 에서 확인할 수 GitHub 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

class ApiGatewayToService: """ Encapsulates Amazon API Gateway functions that are used to create a REST API that integrates with another AWS service. """ def __init__(self, apig_client): """ :param apig_client: A Boto3 API Gateway client. """ self.apig_client = apig_client self.api_id = None self.root_id = None self.stage = None def add_integration_method( self, resource_id, rest_method, service_endpoint_prefix, service_action, service_method, role_arn, mapping_template, ): """ Adds an integration method to a REST API. An integration method is a REST resource, such as '/users', and an HTTP verb, such as GET. The integration method is backed by an AWS service, such as Amazon DynamoDB. :param resource_id: The ID of the REST resource. :param rest_method: The HTTP verb used with the REST resource. :param service_endpoint_prefix: The service endpoint that is integrated with this method, such as 'dynamodb'. :param service_action: The action that is called on the service, such as 'GetItem'. :param service_method: The HTTP method of the service request, such as POST. :param role_arn: The Amazon Resource Name (ARN) of a role that grants API Gateway permission to use the specified action with the service. :param mapping_template: A mapping template that is used to translate REST elements, such as query parameters, to the request body format required by the service. """ service_uri = ( f"arn:aws:apigateway:{self.apig_client.meta.region_name}" f":{service_endpoint_prefix}:action/{service_action}" ) try: self.apig_client.put_method( restApiId=self.api_id, resourceId=resource_id, httpMethod=rest_method, authorizationType="NONE", ) self.apig_client.put_method_response( restApiId=self.api_id, resourceId=resource_id, httpMethod=rest_method, statusCode="200", responseModels={"application/json": "Empty"}, ) logger.info("Created %s method for resource %s.", rest_method, resource_id) except ClientError: logger.exception( "Couldn't create %s method for resource %s.", rest_method, resource_id ) raise try: self.apig_client.put_integration( restApiId=self.api_id, resourceId=resource_id, httpMethod=rest_method, type="AWS", integrationHttpMethod=service_method, credentials=role_arn, requestTemplates={"application/json": json.dumps(mapping_template)}, uri=service_uri, passthroughBehavior="WHEN_NO_TEMPLATES", ) self.apig_client.put_integration_response( restApiId=self.api_id, resourceId=resource_id, httpMethod=rest_method, statusCode="200", responseTemplates={"application/json": ""}, ) logger.info( "Created integration for resource %s to service URI %s.", resource_id, service_uri, ) except ClientError: logger.exception( "Couldn't create integration for resource %s to service URI %s.", resource_id, service_uri, ) raise
  • API에 대한 자세한 내용은 파이썬용AWS SDK (Boto3) API 레퍼런스를 참조하십시오 PutIntegrationResponse.