

# 백엔드 Lambda 함수의 비동기 호출 설정
<a name="set-up-lambda-integration-async"></a>

Lambda 비프록시(사용자 지정)에서, 백엔드 Lambda 함수는 기본적으로 동기적으로 호출됩니다. 이 동작은 대부분의 REST API 작업에 적합합니다. 그러나 일부 애플리케이션은 일반적으로 별도의 백엔드 구성 요소에 의해 (배치 작업 또는 장기 지연 작업으로) 비동기적으로 작업을 수행해야 합니다. 이 경우 백엔드 Lambda 함수는 비동기적으로 호출되며 프런트엔드 REST API 메서드가 결과를 반환하지 않습니다.

[Lambda 호출 유형](https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html)을 `'Event'`로 지정하여 Lambda 함수를 Lambda 비프록시 통합이 비동기적으로 호출되도록 구성할 수 있습니다. 구성 방법은 다음과 같습니다.

## API Gateway 콘솔에서 Lambda 비동기 호출 구성
<a name="asynchronous-invocation-console-examples"></a>

모든 호출이 비동기식 인 경우:
+ **통합 요청**에서 정적 값이 `'Event'`인 `X-Amz-Invocation-Type` 헤더를 추가합니다.

호출이 비동기식인지 동기식인지 여부를 클라이언트가 결정하려면 다음을 수행하세요.

1. **메서드 요청**에서 `InvocationType` 헤더를 추가합니다.

1. **통합 요청**에서 매핑 표현식이 `method.request.header.InvocationType`인 `X-Amz-Invocation-Type` 헤더를 추가합니다.

1. 클라이언트는 API 요청에 비동기식 호출의 경우 `InvocationType: Event` 헤더를, 동기식 호출의 경우 `InvocationType: RequestResponse` 헤더를 포함할 수 있습니다.

## OpenAway를 사용하여 Lambda 비동기 호출 구성
<a name="asynchronous-invocation-OpenAPI-examples"></a>

모든 호출이 비동기식 인 경우:
+  **x-amazon-apigateway-integration** 섹션에 `X-Amz-Invocation-Type` 헤더를 추가합니다.

  ```
  "x-amazon-apigateway-integration" : {
            "type" : "aws",
            "httpMethod" : "POST",
            "uri" : "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-2:123456789012:function:my-function/invocations",
            "responses" : {
              "default" : {
                "statusCode" : "200"
              }
            },
            "requestParameters" : {
              "integration.request.header.X-Amz-Invocation-Type" : "'Event'"
            },
            "passthroughBehavior" : "when_no_match",
            "contentHandling" : "CONVERT_TO_TEXT"
          }
  ```

호출이 비동기식인지 동기식인지 여부를 클라이언트가 결정하려면 다음을 수행하세요.

1.  모든 [OpenAPI Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathItemObject)(OpenAPI 경로 항목 객체)에 다음 헤더를 추가합니다.

   ```
   "parameters" : [ {
   "name" : "InvocationType",
   "in" : "header",
   "schema" : {
     "type" : "string"
   }
   } ]
   ```

1.  **x-amazon-apigateway-integration** 섹션에 `X-Amz-Invocation-Type` 헤더를 추가합니다.

   ```
   "x-amazon-apigateway-integration" : {
             "type" : "aws",
             "httpMethod" : "POST",
             "uri" : "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-2:123456789012:function:my-function/invocations",
             "responses" : {
               "default" : {
                 "statusCode" : "200"
               }
             },
             "requestParameters" : {
               "integration.request.header.X-Amz-Invocation-Type" : "method.request.header.InvocationType"
             },
             "passthroughBehavior" : "when_no_match",
             "contentHandling" : "CONVERT_TO_TEXT"
           }
   ```

1.  클라이언트는 API 요청에 비동기식 호출의 경우 `InvocationType: Event` 헤더를, 동기식 호출의 경우 `InvocationType: RequestResponse` 헤더를 포함할 수 있습니다.

## CloudFormation을 사용하여 Lambda 비동기 간접 호출을 구성합니다.
<a name="asynchronous-invocation-cfn-examples"></a>

다음 CloudFormation 템플릿은 비동기 호출을 위해 `AWS::ApiGateway::Method`를 구성하는 방법을 보여줍니다.

모든 호출이 비동기식 인 경우:

```
AsyncMethodGet:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      RestApiId: !Ref Api
      ResourceId: !Ref AsyncResource
      HttpMethod: GET
      ApiKeyRequired: false
      AuthorizationType: NONE
      Integration:
        Type: AWS
        RequestParameters:
          integration.request.header.X-Amz-Invocation-Type: "'Event'"
        IntegrationResponses:
            - StatusCode: '200'
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${myfunction.Arn}$/invocations
      MethodResponses:
        - StatusCode: '200'
```

호출이 비동기식인지 동기식인지 여부를 클라이언트가 결정하려면 다음을 수행하세요.

```
AsyncMethodGet:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      RestApiId: !Ref Api
      ResourceId: !Ref AsyncResource
      HttpMethod: GET
      ApiKeyRequired: false
      AuthorizationType: NONE
      RequestParameters:
        method.request.header.InvocationType: false
      Integration:
        Type: AWS
        RequestParameters:
          integration.request.header.X-Amz-Invocation-Type: method.request.header.InvocationType
        IntegrationResponses:
            - StatusCode: '200'
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${myfunction.Arn}$/invocations
      MethodResponses:
        - StatusCode: '200'
```

 클라이언트는 API 요청에 비동기식 호출의 경우 `InvocationType: Event` 헤더를, 동기식 호출의 경우 `InvocationType: RequestResponse` 헤더를 포함할 수 있습니다.