

# AWS CloudFormation を使用したレイヤー
<a name="layers-cfn"></a>

CloudFormation を使用してレイヤーを作成し、そのレイヤーを Lambda 関数に関連付けることができます。次のテンプレートの例では、`my-lambda-layer` という名前のレイヤーを作成し、そのレイヤーを **Layers** プロパティを使用して Lambda 関数にアタッチします。

この例では、テンプレートは既存の IAM [実行ロール](lambda-intro-execution-role.md)の Amazon リソースネーム (ARN) を指定します。CloudFormation [AWS::IAM::Role](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html) リソースを使用して、テンプレートに新しい実行ロールを作成することもできます。

関数には、レイヤーを使用するための特別なアクセス許可は必要ありません。

```
---
Description: CloudFormation Template for Lambda Function with Lambda Layer
Resources:
  MyLambdaLayer:
    Type: AWS::Lambda::LayerVersion
    Properties:
      LayerName: my-lambda-layer
      Description: My Lambda Layer
      Content:
        S3Bucket: amzn-s3-demo-bucket
        S3Key: my-layer.zip
      CompatibleRuntimes:
        - python3.9
        - python3.10
        - python3.11

  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: my-lambda-function
      Runtime: python3.9
      Handler: index.handler
      Timeout: 10
      Role: arn:aws:iam::111122223333:role/my_lambda_role
      Layers:
        - !Ref MyLambdaLayer
```