

# AWS CLI를 사용하여 S3 버킷에 로컬 아티팩트 업로드
<a name="using-cfn-cli-package"></a>

AWS CLI를 사용하여 CloudFormation 템플릿에서 참조되는 로컬 아티팩트를 Amazon S3 버킷에 업로드할 수 있습니다. 로컬 아티팩트는 템플릿에서 참조하는 파일입니다. 파일을 S3 버킷에 수동으로 업로드한 다음 템플릿에 위치를 추가하는 대신, 템플릿에서 로컬 아티팩트를 지정하고 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 명령을 사용하여 빠르게 업로드할 수 있습니다.

로컬 아티팩트는 **package** 명령을 통해 Amazon S3에 업로드하는 파일 또는 폴더의 경로입니다. 예를 들어 아티팩트는 AWS Lambda 함수의 소스 코드 또는 Amazon API Gateway REST API의 OpenAPI 파일에 대한 경로일 수 있습니다.

**package** 명령을 사용하는 경우:
+ 파일을 지정하면 이 명령은 해당 파일을 S3 버킷에 직접 업로드합니다.
+ 폴더를 지정하면 이 명령은 폴더에 대한 `.zip` 파일을 생성한 다음 `.zip` 파일을 업로드합니다.
+ 경로를 지정하지 않으면 이 명령은 작업 디렉터리에 대한 `.zip` 파일을 생성하고 이 파일을 업로드합니다.

절대 경로 또는 상대 경로를 지정할 수 있습니다. 여기서 상대 경로는 템플릿 위치에 상대적입니다.

아티팩트를 업로드한 후 이 명령은 로컬 아티팩트에 대한 참조를 아티팩트를 업로드한 S3 위치로 바꾸어 템플릿의 사본을 반환합니다. 그런 다음 반환된 템플릿을 사용하여 스택을 생성하거나 업데이트할 수 있습니다.

**참고**  
**package** 명령이 지원하는 리소스 속성에만 로컬 아티팩트를 사용할 수 있습니다. 이 명령 및 지원되는 리소스 속성 목록에 대한 자세한 내용을 알아보려면 [AWS CLI 명령 레퍼런스](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/index.html)의 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 설명서를 참조하세요.



## 사전 조건
<a name="using-cfn-cli-package-prerequisites"></a>

시작하기 전에 기존 Amazon S3 버킷이 있어야 합니다.

## 로컬 아티팩트를 사용하여 템플릿 패키징 및 배포
<a name="package-and-deploy-a-template-with-local-artifacts"></a>

다음 템플릿은 Lambda 함수의 소스 코드에 대한 로컬 아티팩트를 지정합니다. 소스 코드는 `/home/user/code/lambdafunction` 폴더에 저장됩니다.

**원본 템플릿**

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.handler",
        "Runtime": "nodejs18.x",
        "CodeUri": "/home/user/code/lambdafunction"
      }
    }
  }
}
```

다음 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 명령은 함수의 소스 코드 폴더 `.zip` 파일을 지정된 버킷의 루트에 생성 및 업로드합니다.

```
aws cloudformation package \
  --s3-bucket amzn-s3-demo-bucket \
  --template /path_to_template/template.json \
  --output-template-file packaged-template.json \
  --output json
```

명령은 `--output-template-file`에 지정된 경로에서 새 템플릿을 생성합니다. 이는 아래와 같이 아티팩트 참조를 Amazon S3 위치로 바꿉니다. `.zip` 파일의 이름은 폴더 이름 자체를 사용하기보다는 폴더 콘텐츠의 MD5 체크섬을 사용하여 지정됩니다.

**결과 템플릿**

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.handler",
        "Runtime": "nodejs18.x",
        "CodeUri": "s3://amzn-s3-demo-bucket/md5 checksum"
      }
    }
  }
}
```

템플릿의 아티팩트를 패키징한 후 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/) 명령을 사용하여 처리된 템플릿을 배포합니다.

```
aws cloudformation deploy \
  --template-file packaged-template.json \
  --stack-name stack-name
```

51,200바이트를 초과하는 템플릿을 배포할 때는 다음 예제와 같이 **deploy** 명령을 `--s3-bucket` 옵션과 함께 사용하여 템플릿을 S3에 업로드합니다.

```
aws cloudformation deploy \
  --template-file packaged-template.json \
  --stack-name stack-name \
  --s3-bucket amzn-s3-demo-bucket
```

**참고**  
**package** 명령을 사용하여 로컬 아티팩트를 업로드하는 또 다른 예는 [중첩 스택을 사용하여 템플릿을 재사용 가능한 조각으로 분할](using-cfn-nested-stacks.md)을 참조하세요.