

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

# AWS SAM CLI Terraform 지원
<a name="terraform-support"></a>

이 섹션에서는 Terraform 프로젝트 및 Terraform 클라우드에서 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)를 사용하는 방법을 다룹니다.

피드백을 제공하고 기능 요청을 제출하려면 [GitHub 문제](https://github.com/aws/aws-sam-cli/issues/new?labels=area%2Fterraform)를 생성하세요.

**Topics**
+ [AWS SAM CLI에 대한 Terraform 지원과 함께 시작하기](gs-terraform-support.md)
+ [로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기](using-samcli-terraform.md)
+ [로컬 디버깅 및 테스트에 Serverless.tf와 함께 AWS SAMCLI 사용](using-samcli-serverlesstf.md)
+ [Terraform 참조로AWS SAMCLI 사용](terraform-reference.md)
+ [Terraform에 대한 AWS SAMCLI지원이란 무엇입니까?](#what-is-terraform-support)

# AWS SAM CLI에 대한 Terraform 지원과 함께 시작하기
<a name="gs-terraform-support"></a>

이 주제에서는에서 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI) 사용을 시작하는 방법을 다룹니다Terraform.

피드백을 제공하고 기능 요청을 제출하려면 [GitHub 문제](https://github.com/aws/aws-sam-cli/issues/new?labels=area%2Fterraform)를 생성하세요.

**Topics**
+ [AWS SAM CLI Terraform사전 조건](#gs-terraform-support-prerequisites)
+ [Terraform과 함께 AWS SAMCLI 명령 사용](#gs-terraform-support-using)
+ [Terraform 프로젝트 설정](#gs-terraform-support-projects)
+ [Terraform Cloud에 대해 설정](#gs-terraform-support-cloud)

## AWS SAM CLI Terraform사전 조건
<a name="gs-terraform-support-prerequisites"></a>

Terraform 프로젝트에서 AWS SAMCLI를 사용하려면 모든 사전 조건을 완료하세요.

1. 

**AWS SAM CLI 설치 또는 업그레이드**

    AWS SAMCLI가 설치되어 있는지 확인하려면 다음을 실행합니다.

   ```
   $ sam --version
   ```

    AWS SAMCLI가 이미 설치된 경우 출력에 버전이 표시됩니다. 최신 버전으로 업그레이드하려면 [AWS SAMCLI업그레이드](manage-sam-cli-versions.md#manage-sam-cli-versions-upgrade) 섹션을 참조하세요.

   모든 사전 조건과 함께 AWS SAMCLI를 설치하는 것에 대한 지침은 [AWS SAM CLI 설치](install-sam-cli.md) 섹션을 참조하세요.

1. 

**Terraform 설치**

   Terraform 설치 여부를 확인하려면 다음을 실행합니다.

   ```
   $ terraform -version
   ```

   Terraform을 설치하려면 *Terraform 레지스트리*에서 [Terraform 설치](https://developer.hashicorp.com/terraform/downloads)를 참조하세요.

1. 

**로컬 테스트를 위한 Docker 설치**

    AWS SAMCLI에는 로컬 테스트를 위해 Docker가 필요합니다. Docker을 설치하려면 [AWS SAMCLI와 함께 사용할 Docker 설치](install-docker.md) 섹션을 참조하세요.

## Terraform과 함께 AWS SAMCLI 명령 사용
<a name="gs-terraform-support-using"></a>

지원되는 AWS SAMCLI 명령을 실행할 때는 `--hook-name` 옵션을 사용하고 `terraform` 값을 제공합니다. 다음은 예제입니다.

```
$ sam local invoke --hook-name terraform
```

다음과 같이 AWS SAMCLI 구성 파일에서 이 옵션을 구성할 수 있습니다.

```
hook_name = "terraform"
```

## Terraform 프로젝트 설정
<a name="gs-terraform-support-projects"></a>

Terraform 프로젝트에서 AWS SAM CLI를 사용하려면 이 항목의 단계를 완료합니다.

Terraform 프로젝트 외부에서 AWS Lambda 아티팩트를 빌드하는 경우 추가 설정이 필요하지 않습니다. 사용을 [로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기](using-samcli-terraform.md) 시작하려면 단원을 참조하십시오 AWS SAM CLI.

Terraform 프로젝트 내에 Lambda 아티팩트을 빌드하는 경우 다음을 수행해야 합니다.

1. Python 3.8 이상 버전 설치

1. Make 도구를 설치합니다.

1. Terraform 프로젝트 내에서 Lambda 아티팩트 빌드 로직을 정의합니다.

1. 빌드 로직의 AWS SAMCLI를 알릴 `sam metadata` 리소스를 정의합니다.

1. `sam build` 명령을 사용하여 Lambda 아티팩트를 AWS SAM CLI 빌드합니다.

### Python 3.8 이상 버전 설치
<a name="gs-terraform-support-projects-python"></a>

Python에서 사용하려면 3.8 이상이 필요합니다 AWS SAM CLI. `sam build`를 실행하면 AWS SAMCLI가 Lambda 아티팩트를 빌드하기 위한 Python 명령이 포함된 `makefiles`를 생성합니다.

설치 지침은 Python *초급 가이드*의 [Python 다운로드](https://wiki.python.org/moin/BeginnersGuide/Download) 섹션을 참조하세요.

다음을 실행하여 Python 3.8 이상이 컴퓨터 경로에 추가되었는지 확인합니다.

```
$ python --version
```

출력에는 3.8 이상의 Python 버전이 표시되어야 합니다.

### Make 도구를 설치합니다.
<a name="gs-terraform-support-projects-make"></a>

GNU [Make](https://www.gnu.org/software/make/)(은)는 프로젝트의 실행 파일 및 기타 비소스 파일 생성을 제어하는 도구입니다. AWS SAMCLI는 이 도구를 사용하여 Lambda 아티팩트를 빌드하는 `makefiles`를 생성합니다.

Make가 로컬 시스템에 아직 설치되지 않은 경우, 계속 진행하기 전에 설치하세요.

Windows의 경우 [Chocolatey](https://chocolatey.org/)를 사용하여 설치할 수 있습니다. 자세한 지침은 *Windows에서 ‘Make’를 설치하고 사용하는 방법*의 [Chocolatey 사용하기](https://www.technewstoday.com/install-and-use-make-in-windows/#using-chocolatey) 섹션을 참조하세요.

### Lambda 아티팩트 빌드 로직 정의
<a name="gs-terraform-support-projects-logic"></a>

`null_resource` Terraform 리소스 유형을 사용하여 Lambda 빌드 로직을 정의합니다. 다음은 사용자 지정 빌드 스크립트를 사용하여 Lambda 함수를 빌드하는 예제입니다.

```
resource "null_resource" "build_lambda_function" {
    triggers = {
        build_number = "${timestamp()}" 
    }

    provisioner "local-exec" {
        command =  substr(pathexpand("~"), 0, 1) == "/"? "./py_build.sh \"${local.lambda_src_path}\" \"${local.building_path}\" \"${local.lambda_code_filename}\" Function" : "powershell.exe -File .\\PyBuild.ps1 ${local.lambda_src_path} ${local.building_path} ${local.lambda_code_filename} Function"
    }
}
```

### sam metadata 리소스를 정의합니다.
<a name="gs-terraform-support-projects-metadata"></a>

`sam metadata` 리소스는 AWS SAMCLI에 Lambda 아티팩트를 찾는 데 필요한 정보를 제공하는 `null_resource` Terraform 리소스 유형입니다. 프로젝트의 각 Lambda 함수 또는 계층에는 고유한 `sam metadata` 리소스가 필요합니다. 이 리소스 유형에 대해 자세히 알아보려면 *Terraform 레지스트리*의 [null\$1resource](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource)를 참조하세요.

**sam metadata 리소스를 정의하려면**

1. 리소스가 sam metadata 리소스임을 식별하는 `sam_metadata_`로 시작하는 리소스의 이름을 지정합니다.

1. 리소스의 `triggers` 블록 내에서 Lambda 아티팩트 속성을 정의합니다.

1. `depends_on` 인수와 함께 Lambda 빌드 로직을 포함하는 `null_resource`를 지정합니다.

   다음은 템플릿의 예입니다.

   ```
   resource "null_resource" "sam_metadata_..." {
     triggers = {
       resource_name = resource_name
       resource_type = resource_type
       original_source_code = original_source_code
       built_output_path = built_output_path
     }
     depends_on = [
       null_resource.build_lambda_function # ref to your build logic
     ]
   }
   ```

   다음은 `sam metadata` 리소스의 예입니다.

   ```
   resource "null_resource" "sam_metadata_aws_lambda_function_publish_book_review" {
       triggers = {
           resource_name = "aws_lambda_function.publish_book_review"
           resource_type = "ZIP_LAMBDA_FUNCTION"
           original_source_code = "${local.lambda_src_path}"
           built_output_path = "${local.building_path}/${local.lambda_code_filename}"
       }
       depends_on = [
           null_resource.build_lambda_function
       ]
   }
   ```

`sam metadata` 리소스의 콘텐츠는 Lambda 리소스 유형(함수 또는 계층)과 패키지 유형(ZIP 또는 이미지)에 따라 달라집니다. 예에 사용된 SSML에 대한 자세한 내용은 [sam 메타데이터 리소스](terraform-sam-metadata.md) 섹션을 참조하세요.

`sam metadata` 리소스를 구성하고 지원되는 AWS SAMCLI 명령을 사용하면 AWS SAMCLI는 AWS SAMCLI 명령을 실행하기 전에 메타데이터 파일을 생성합니다. 이 파일을 생성한 후에는 미래 AWS SAMCLI 명령과 함께 `--skip-prepare-infra` 옵션을 사용하여 메타데이터 생성 프로세스를 건너뛰고 시간을 절약할 수 있습니다. 이 옵션은 새 Lambda 함수 또는 새 API 엔드포인트 생성과 같이 인프라를 변경하지 않은 경우에만 사용해야 합니다.

### AWS SAMCLI를 사용하여 Lambda 아티팩트를 빌드합니다.
<a name="gs-terraform-support-projects-build"></a>

`sam build` 명령을 사용하여 Lambda 아티팩트를 AWS SAM CLI 빌드합니다. `sam build`를 실행하면 AWS SAMCLI는 다음과 같은 작업을 수행합니다.

1. Lambda 리소스에 대해 알아보고 위치를 찾기 위해 Terraform 프로젝트에서 `sam metadata` 리소스를 찾습니다.

1. Lambda 빌드 로직을 시작하여 Lambda 아티팩트를 빌드합니다.

1. `sam local` 명령과 함께 사용할 Terraform 프로젝트를 구성하는 `.aws-sam` 디렉터리를 AWS SAM CLI 생성합니다.

**sam build로 빌드하려면**

1. Terraform 루트 모듈이 포함된 디렉터리에서 다음을 실행합니다.

   ```
   $ sam build --hook-name terraform
   ```

1. 특정 Lambda 함수 또는 계층을 빌드하려면 다음을 실행합니다.

   ```
   $ sam build --hook-name terraform lambda-resource-id
   ```

   Lambda 리소스 ID는 `aws_lambda_function.list_books` 또는 `module.list_book_function.aws_lambda_function.this[0]`과 같은 Lambda 함수 이름 또는 전체 Terraform 리소스 주소일 수 있습니다.

함수 소스 코드나 기타 Terraform 구성 파일이 Terraform 루트 모듈이 포함된 디렉터리 외부에 있는 경우 위치를 지정해야 합니다. `--terraform-project-root-path` 옵션을 사용하여 이러한 파일이 포함된 최상위 디렉터리의 절대 경로 또는 상대 경로를 지정합니다. 다음은 예제입니다.

```
$ sam build --hook-name terraform --terraform-project-root-path ~/projects/terraform/demo
```

#### 컨테이너를 사용하여 빌드하기
<a name="gs-terraform-support-projects-build-container"></a>

`sam build` 명령을 실행할 AWS SAM CLI 때 로컬 Docker 컨테이너를 AWS SAM CLI 사용하여 애플리케이션을 빌드하도록를 구성할 수 있습니다.

**참고**  
Docker를 설치하고 구성합니다. 지침은 [AWS SAMCLI와 함께 사용할 Docker 설치](install-docker.md) 섹션을 참조하세요.

**컨테이너를 사용하여 빌드하려면**

1. Terraform,Python, Make 도구가 포함된 `Dockerfile`를 만듭니다. Lambda 함수 런타임도 포함해야 합니다.

   다음은 한 `Dockerfile` 예입니다.

   ```
   FROM public.ecr.aws/amazonlinux/amazonlinux:2
   
   RUN yum -y update \
       && yum install -y unzip tar gzip bzip2-devel ed gcc gcc-c++ gcc-gfortran \
       less libcurl-devel openssl openssl-devel readline-devel xz-devel \
       zlib-devel glibc-static libcxx libcxx-devel llvm-toolset-7 zlib-static \
       && rm -rf /var/cache/yum
   
   RUN yum -y install make \
       && yum -y install zip
   
   RUN yum install -y yum-utils \
       && yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo \
       && yum -y install terraform \
       && terraform --version
   
   # AWS Lambda Builders
   RUN amazon-linux-extras enable python3.8
   RUN yum clean metadata && yum -y install python3.8
   RUN curl -L get-pip.io | python3.8
   RUN pip3 install aws-lambda-builders
   RUN ln -s /usr/bin/python3.8 /usr/bin/python3
   RUN python3 --version
   
   VOLUME /project
   WORKDIR /project
   
   ENTRYPOINT ["sh"]
   ```

1. [https://docs.docker.com/engine/reference/commandline/build/](https://docs.docker.com/engine/reference/commandline/build/)를 사용하여 Docker 이미지를 빌드합니다.

   다음은 예제입니다.

   ```
   $ docker build --tag terraform-build:v1 <path-to-directory-containing-Dockerfile>
   ```

1. `--use-container` 및 `--build-image` 옵션을 사용하여 `sam build` 명령을 실행합니다 AWS SAM CLI.

   다음은 예제입니다.

   ```
   $ sam build --use-container --build-image terraform-build:v1
   ```

### 다음 단계
<a name="gs-terraform-support-projects-next"></a>

Terraform 프로젝트에서 [로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기](using-samcli-terraform.md) AWS SAM를 사용하려면 CLI 섹션을 참조하세요.

## Terraform Cloud에 대해 설정
<a name="gs-terraform-support-cloud"></a>

Terraform v1.6.0 버전 이상을 사용하는 것이 좋습니다. 이전 버전을 사용하는 경우 로컬에서 Terraform 계획 파일을 생성해야 합니다. 로컬 계획 파일은 로컬 테스트 및 디버깅을 수행하는 데 필요한 정보를에 제공합니다 AWS SAM CLI.

**로컬 계획 파일을 생성하려면**
**참고**  
Terraform v1.6.0 이후의 버전에는 이러한 단계가 필요하지 않습니다. 에서 AWS SAM CLI 사용을 시작하려면 단원을 Terraform Cloud참조하십시오[Terraform과 함께 AWS SAM CLI 사용](using-samcli-terraform.md).

1. **API 토큰 구성** - 토큰 유형은 액세스 수준에 따라 달라집니다. 자세한 내용은 [API 토큰](https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/api-tokens)의 *Terraform Cloud 배문서*를 참조하세요.

1. **API 토큰 환경 변수 설정** - 다음은 명령줄의 예입니다.

   ```
   $ export TOKEN="<api-token-value>"
   ```

1. **실행 ID 가져오기** - Terraform Cloud 콘솔에서에 사용할 실행의 Terraform 실행 ID를 찾습니다 AWS SAM CLI.

   실행 ID는 실행의 브레드크럼 경로에 있습니다.  
![\[실행 ID를 표시하는 Terraform Cloud의 브레드크럼 경로.\]](http://docs.aws.amazon.com/ko_kr/serverless-application-model/latest/developerguide/images/terraform-01.png)

1. **계획 파일 가져오기** - API 토큰을 사용하여 로컬 계획 파일을 가져옵니다. 다음은 명령줄의 예입니다.

   ```
   curl \
      --header "Authorization: Bearer $TOKEN" \
      --header "Content-Type: application/vnd.api+json" \
      --location \
      https://app.terraform.io/api/v2/runs/<run ID>/plan/json-output \
      > custom_plan.json
   ```

이제 Terraform Cloud와 함께 AWS SAM CLI를 사용할 준비가 되었습니다. 지원되는 AWS SAMCLI 명령을 사용하는 경우 `--terraform-plan-file` 옵션을 사용하여 로컬 계획 파일의 이름과 경로를 지정합니다. 다음은 예제입니다.

```
$ sam local invoke --hook-name terraform --terraform-plan-file custom-plan.json
```

다음은 `sam local start-api`를 사용한 명령의 예입니다.

```
$ sam local start-api --hook-name terraform --terraform-plan-file custom-plan.json
```

이러한 예제와 함께 사용할 수 있는 샘플 애플리케이션은 *aws-samples GitHub 리포지토리*의 [api\$1gateway\$1v2\$1tf\$1cloud](https://github.com/aws-samples/aws-sam-terraform-examples/tree/main/ga/api_gateway_v2_tf_cloud)를 참조하세요.

### 다음 단계
<a name="gs-terraform-support-cloud-next"></a>

Terraform Cloud와 함께 AWS SAMCLI를 시작하려면 [로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기](using-samcli-terraform.md) 섹션을 참조하세요.

# 로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기
<a name="using-samcli-terraform"></a>

이 주제에서는 Terraform 프로젝트 및에서 지원되는 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI) 명령을 사용하는 방법을 다룹니다Terraform Cloud.

피드백을 제공하고 기능 요청을 제출하려면 [GitHub 문제](https://github.com/aws/aws-sam-cli/issues/new?labels=area%2Fterraform)를 생성하세요.

**Topics**
+ [sam local invoke를 사용한 로컬 테스트](#using-samcli-terraform-local-invoke)
+ [sam local start-api를 사용한 로컬 테스트](#using-samcli-terraform-local-start-api)
+ [sam local start-lambda를 사용한 로컬 테스트](#using-samcli-terraform-local-start-lambda)
+ [Terraform 제한 사항](#using-samcli-terraform-unsupported)

## sam local invoke를 사용한 로컬 테스트
<a name="using-samcli-terraform-local-invoke"></a>

**참고**  
 AWS SAMCLI를 사용하여 로컬에서 테스트하려면 Docker를 설치하고 구성해야 합니다. 지침은 [AWS SAMCLI와 함께 사용할 Docker 설치](install-docker.md) 섹션을 참조하세요.

다음은 이벤트를 전달하여 로컬에서 Lambda 함수를 테스트하는 예입니다.

```
$ sam local invoke --hook-name terraform hello_world_function -e events/event.json -
```

이 함수 사용에 대한 자세한 내용은 [sam local invoke를 사용한 테스트 소개](using-sam-cli-local-invoke.md) 섹션을 참조하세요.

## sam local start-api를 사용한 로컬 테스트
<a name="using-samcli-terraform-local-start-api"></a>

Terraform과 함께 `sam local start-api`를 사용하려면 다음이 필요합니다.

```
$ sam local start-api --hook-name terraform
```

다음은 예제입니다.

```
$ sam local start-api --hook-name terraform                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                          
Running Prepare Hook to prepare the current application                                                                                                                                                   
Executing prepare hook of hook "terraform"                                                                                                                                                                
Initializing Terraform application                                                                                                                                                                        
...
Creating terraform plan and getting JSON output                                                                                                                                                           
....
Generating metadata file                                                                                                                                                                                  
                                                                                                                                                                                                          
Unresolvable attributes discovered in project, run terraform apply to resolve them.                                                                                                                       
                                                                                                                                                                                                          
Finished generating metadata file. Storing in...
Prepare hook completed and metadata file generated at: ...    
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]                                                                                                                                          
Mounting None at http://127.0.0.1:3000/hello [POST]                                                                                                                                                       
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you  
used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template                     
2023-06-26 13:21:20  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
```

[sam local start-api를 사용한 테스트 소개](using-sam-cli-local-start-api.md) 명령에 대한 자세한 내용은 섹션을 참조하세요.

### Lambda 권한 부여자를 사용하는 Lambda 함수
<a name="using-sam-cli-terraform-local-start-api-authorizers"></a>

Lambda 권한 부여자를 사용하도록 구성된 Lambda 함수의 경우 AWS SAMCLI는 Lambda 함수 엔드포인트를 간접 호출하기 전에 Lambda 권한 부여자를 자동으로 간접 호출합니다.
+ 에서이 기능에 대해 자세히 알아보려면 섹션을 AWS SAM CLI참조하세요[Lambda 권한 부여자를 사용하는 Lambda 함수](using-sam-cli-local-start-api.md#using-sam-cli-local-start-api-authorizers).
+ Terraform에서 Lambda 권한 부여자를 사용하는 방법에 대한 자세한 내용은 *Terraform 레지스트리*에서 [https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer#example-usage](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer#example-usage) 섹션을 참조하세요.

## sam local start-lambda를 사용한 로컬 테스트
<a name="using-samcli-terraform-local-start-lambda"></a>

다음은 AWS Command Line Interface ()를 사용하여 로컬에서 Lambda 함수를 테스트하는 예입니다.AWS CLI

1.  AWS SAMCLI를 사용하여 로컬 테스트 환경을 생성합니다.

   ```
   $ sam local start-lambda --hook-name terraform hello_world_function
   ```

1.  AWS CLI 를 사용하여 로컬에서 함수를 호출합니다.

   ```
   $ aws lambda invoke --function-name hello_world_function --endpoint-url http://127.0.0.1:3001/ response.json --cli-binary-format raw-in-base64-out --payload file://events/event.json
   ```

[sam local start-lambda를 사용한 테스트 소개](using-sam-cli-local-start-lambda.md) 명령에 대한 자세한 내용은 섹션을 참조하세요.

## Terraform 제한 사항
<a name="using-samcli-terraform-unsupported"></a>

Terraform와 함께 AWS SAM CLI를 사용할 때의 제한 사항은 다음과 같습니다.
+ Lambda 함수는 여러 계층에 연결되어 있습니다.
+ 리소스 간 링크를 정의하는 Terraform 로컬 변수.
+ 아직 생성되지 않은 Lambda 함수를 참조합니다. 여기에는 REST API 리소스의 본문 속성에 정의된 함수가 포함됩니다.

이러한 제한을 피하려면 새 리소스가 추가될 때 `terraform apply`를 실행할 수 있습니다.

# 로컬 디버깅 및 테스트에 Serverless.tf와 함께 AWS SAMCLI 사용
<a name="using-samcli-serverlesstf"></a>

 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)는 AWS Lambda 함수 및 계층의 로컬 디버깅 및 테스트를 위해 Serverless.tf 모듈과 함께 사용할 수 있습니다. 다음 AWS SAMCLI 명령이 지원됩니다.
+ `sam build`
+ `sam local invoke`
+ `sam local start-api`
+ `sam local start-lambda`

**참고**  
Serverless.tf 버전 4.6.0 이상은 AWS SAMCLI 통합을 지원합니다.

Serverless.tf 모듈과 함께 사용을 시작하려면 AWS SAM CLI 최신 버전의 Serverless.tf 및 로 업데이트합니다 AWS SAM CLI.

**serverless.tf version 6.0.0**부터 `create_sam_metadata` 파라미터를 `true`로 설정해야 합니다. 이렇게 하면 `sam build` 명령에 필요한 메타데이터 리소스가 AWS SAM CLI 생성됩니다.

Serverless.tf에 대해 자세히 알아보려면 [terraform-aws-lambda-module](https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws/latest)을 참조하세요.

# Terraform 참조로AWS SAMCLI 사용
<a name="terraform-reference"></a>

이 섹션은 로컬 디버깅 및 테스트를 위해 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)를와 함께 사용하기 Terraform 위한 참조입니다.

피드백을 제공하고 기능 요청을 제출하려면 [GitHub 문제](https://github.com/aws/aws-sam-cli/issues/new?labels=area%2Fterraform)를 생성하세요.

## AWS SAM 지원되는 기능 참조
<a name="terraform-reference-sam"></a>

Terraform 사용에 지원되는 AWS SAMCLI 기능에 대한 참조 설명서는 다음에서 찾을 수 있습니다.
+ [sam build](sam-cli-command-reference-sam-build.md)
+ [sam local invoke](sam-cli-command-reference-sam-local-invoke.md)
+ [sam local start-api](sam-cli-command-reference-sam-local-start-api.md)
+ [sam local start-lambda](sam-cli-command-reference-sam-local-start-lambda.md)

## Terraform 특정 참조
<a name="terraform-reference-specific"></a>

Terraform과 함께 AWS SAMCLI 사용과 관련된 참조 문서는 다음에서 찾을 수 있습니다.
+ [sam 메타데이터 리소스](terraform-sam-metadata.md)

# sam 메타데이터 리소스
<a name="terraform-sam-metadata"></a>

이 페이지에는 Terraform 프로젝트에 사용되는 **sam metadata resource** 리소스 유형에 대한 참조 정보를 포함합니다.
+ 에서 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)를 사용하는 방법에 대한 소개는 섹션을 Terraform참조하세요[Terraform에 대한 AWS SAMCLI지원이란 무엇입니까?](terraform-support.md#what-is-terraform-support).
+ Terraform과 함께 AWS SAM CLI을 사용하려면 [로컬 디버깅 및 테스트에 Terraform과 함께 AWS SAMCLI 사용하기](using-samcli-terraform.md) 섹션을 참조하세요.

**Topics**
+ [인수](#terraform-sam-metadata-arguments)
+ [예제](#terraform-sam-metadata-examples)

## 인수
<a name="terraform-sam-metadata-arguments"></a>


****  

| 인수 | 설명 | 
| --- | --- | 
| built\$1output\$1path |  AWS Lambda 함수의 빌드된 아티팩트 경로입니다. | 
| docker\$1build\$1args | Docker 빌드 인수 JSON 객체의 디코딩된 문자열. 이 인수는 선택 사항입니다. | 
| docker\$1context | 도커 이미지 빌드 컨텍스트가 포함된 디렉터리의 경로. | 
| docker\$1file |  Docker 파일에 대한 경로입니다. 이 경로는 `docker_context` 경로와 상대적입니다. 이 인수는 선택 사항입니다. 기본값은 `Dockerfile`입니다.  | 
| docker\$1tag | 생성된 도커 이미지 태그의 값. 이 값은 선택 사항입니다. | 
| depends\$1on | Lambda 함수 또는 계층의 빌딩 리소스 경로. 자세히 알아보려면Terraform 레지스트리의 [**depends\$1on** 인수](https://developer.hashicorp.com/terraform/language/meta-arguments/depends_on)를 참조하세요. | 
| original\$1source\$1code |  Lambda 함수가 정의된 경로입니다. 이 값은 문자열, 문자열 배열 또는 문자열로 디코딩된 JSON 객체일 수 있습니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/serverless-application-model/latest/developerguide/terraform-sam-metadata.html)  | 
| resource\$1name | Lambda 함수의 이름입니다. | 
| resource\$1type |  Lambda 함수 패키지 유형의 형식. 사용 가능한 값은 다음과 같습니다. [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/serverless-application-model/latest/developerguide/terraform-sam-metadata.html)  | 
| source\$1code\$1property | JSON 객체에 있는 Lambda 리소스 코드의 경로입니다. original\$1source\$1code가 JSON 객체인 경우 이 속성을 정의합니다. | 

## 예제
<a name="terraform-sam-metadata-examples"></a>

### ZIP 패키지 유형을 사용하는 Lambda 함수를 참조하는 sam metadata resource
<a name="terraform-sam-metadata-examples-example1"></a>

```
# Lambda function resource
resource "aws_lambda_function" "tf_lambda_func" {
  filename = "${path.module}/python/hello-world.zip"
  handler = "index.lambda_handler"
  runtime = "python3.8"
  function_name = "function_example"
  role = aws_iam_role.iam_for_lambda.arn
  depends_on = [
    null_resource.build_lambda_function # function build logic
  ]
}

# sam metadata resource
resource "null_resource" "sam_metadata_function_example" {
  triggers = {
    resource_name = "aws_lambda_function.function_example"
    resource_type = "ZIP_LAMBDA_FUNCTION"
    original_source_code = "${path.module}/python"
    built_output_path = "${path.module}/building/function_example"
  }
  depends_on = [
    null_resource.build_lambda_function # function build logic
  ]
}
```

### 이미지 패키지 유형을 사용하여 Lambda 함수를 참조하는 sam 메타데이터 리소스
<a name="terraform-sam-metadata-examples-example2"></a>

```
resource "null_resource" "sam_metadata_function {
  triggers = {
    resource_name = "aws_lambda_function.image_function"
    resource_type = "IMAGE_LAMBDA_FUNCTION"
    docker_context = local.lambda_src_path
    docker_file = "Dockerfile"
    docker_build_args = jsonencode(var.build_args)
    docker_tag = "latest"
  }
}
```

### Lambda 계층을 참조하는 sam 메타데이터 리소스
<a name="terraform-sam-metadata-examples-example3"></a>

```
resource "null_resource" "sam_metadata_layer1" {
  triggers = {
    resource_name = "aws_lambda_layer_version.layer"
    resource_type = "LAMBDA_LAYER"
    original_source_code = local.layer_src
    built_output_path = "${path.module}/${layer_build_path}"
  }
  depends_on = [null_resource.layer_build]
}
```

## Terraform에 대한 AWS SAMCLI지원이란 무엇입니까?
<a name="what-is-terraform-support"></a>

 AWS Serverless Application Model 명령줄 인터페이스(AWS SAM CLI)를 Terraform 프로젝트 또는와 함께 사용하여 다음에 대한 로컬 디버깅 및 테스트를 Terraform Cloud 수행합니다.
+ AWS Lambda 함수 및 계층.
+ Amazon API Gateway API

Terraform에 대한 소개는 *HashiCorp Terraform 웹사이트*에서 [Terraform란 무엇입니까?](https://developer.hashicorp.com/terraform/intro) 섹션을 참조하세요.

피드백을 제공하고 기능 요청을 제출하려면 [GitHub 문제](https://github.com/aws/aws-sam-cli/issues/new?labels=area%2Fterraform)를 생성하세요.

**참고**  
의 AWS SAM CLI통합 구문 분석 단계의 일환으로 AWS SAM CLI는 사용자 명령을 처리하여 프로젝트 파일과 데이터를 생성합니다. 명령 출력은 변경되지 않아야 하지만 특정 환경에서는 환경 또는 러너가 출력에 추가 로그나 정보를 삽입할 수 있습니다.

**Topics**
+ [란 무엇입니까 AWS SAM CLI?](#what-is-terraform-support-sam-cli)
+ [Terraform와 함께 AWS SAM CLI를 사용하려면 어떻게 해야 합니까?](#what-is-terraform-support-how)
+ [다음 단계](#what-is-terraform-support-next)

### 란 무엇입니까 AWS SAM CLI?
<a name="what-is-terraform-support-sam-cli"></a>

는 AWS SAM CLI AWS SAM 템플릿 및와 같은 지원되는 타사 통합과 함께 사용하여 서버리스 애플리케이션을 Terraform빌드하고 실행할 수 있는 명령줄 도구입니다. 에 대한 소개는 단원을 AWS SAM CLI참조하십시오[란 무엇입니까 AWS SAM CLI?](what-is-sam-overview.md#what-is-sam-cli).

는 AWS SAM CLI에 대해 Terraform다음 명령을 지원합니다.
+ `sam local invoke` - 로컬에서 AWS Lambda 함수 리소스의 일회성 호출을 시작합니다. [sam local invoke를 사용한 테스트 소개](using-sam-cli-local-invoke.md) 명령에 대한 자세한 내용은 섹션을 참조하세요.
+ `sam local start-api` - Lambda 리소스를 로컬에서 실행하고 로컬 HTTP 서버 호스트를 통해 테스트합니다. 이 유형의 테스트는 API Gateway 엔드포인트에서 간접 호출되는 Lambda 함수에 유용합니다. [sam local start-api를 사용한 테스트 소개](using-sam-cli-local-start-api.md) 명령에 대한 자세한 내용은 섹션을 참조하세요.
+ `sam local start-lambda` - AWS Command Line Interface (AWS CLI) 또는 SDKs를 사용하여 함수를 로컬로 호출하려면 Lambda 함수의 로컬 엔드포인트를 시작합니다. [sam local start-lambda를 사용한 테스트 소개](using-sam-cli-local-start-lambda.md) 명령에 대한 자세한 내용은 섹션을 참조하세요.

### Terraform와 함께 AWS SAM CLI를 사용하려면 어떻게 해야 합니까?
<a name="what-is-terraform-support-how"></a>

[핵심 Terraform 워크플로](https://developer.hashicorp.com/terraform/intro/core-workflow)는 **작성**, **계획**, **적용**의 3단계로 구성됩니다. 에 대한 지원을 통해 AWS SAM CLI Terraform 워크플로를 AWS SAM CLI 계속 사용하여에서 애플리케이션을 관리하면서 명령 `sam local` 세트를 활용할 Terraform수 있습니다 AWS. 일반적으로 이는 다음을 의미합니다.
+ **작성** - Terraform를 사용하여 인프라를 코드로 작성합니다.
+ **테스트 및 디버그** - AWS SAMCLI를 사용하여 애플리케이션을 로컬에서 테스트하고 디버깅합니다.
+ **계획** - 적용하기 전에 변경 사항을 미리 볼 수 있습니다.
+ **적용** - 인프라를 프로비저닝합니다.

와 함께를 AWS SAM CLI 사용하는 예제는 *AWS 컴퓨팅 블로그*의 [ 함께 개선: AWS SAM CLI 및 HashiCorp Terraform](https://aws.amazon.com/blogs/compute/better-together-aws-sam-cli-and-hashicorp-terraform/) 섹션을 Terraform참조하세요.

### 다음 단계
<a name="what-is-terraform-support-next"></a>

모든 사전 조건을 완료하고 Terraform을 설정하려면 [AWS SAM CLI에 대한 Terraform 지원과 함께 시작하기](gs-terraform-support.md) 섹션을 참조하세요.