

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 CloudFormation 在現有的 Lambda 函數上啟用 Lambda Insights
<a name="Lambda-Insights-Getting-Started-cloudformation"></a>

請依照下列步驟使用 CloudFormation 在現有的 Lambda 函數上啟用 Lambda Insights。

**步驟 1：安裝層**

將 Lambda Insights 層新增至 Lambda Insights 層 ARN 內的 `Layers` 屬性。下面的範例使用 Lambda Insights 的初始版本層。如需 Lambda Insights 延伸層的最新版本，請參閱 [Lambda Insights 延伸的可用版本](Lambda-Insights-extension-versions.md)。

```
Resources:
  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Layers:
        - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14"
```

**步驟 2：新增受管政策**

將 **CloudWatchLambdaInsightsExecutionRolePolicy** IAM 政策新增至您的函數執行角色中。

```
Resources:
  MyFunctionExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy'
```

**步驟 3：(選用) 新增 VPC 端點**

此步驟僅適用於在沒有網際網路存取的私有子網路中執行的函數，以及您尚未設定 CloudWatch Logs Virtual Private Cloud (VPC) 端點時才需要。如需詳細資訊，請參閱[搭配界面 VPC 端點使用 CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch-logs-and-interface-VPC.html)。

```
Resources:
  CloudWatchLogsVpcPrivateEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      PrivateDnsEnabled: 'true'
      VpcEndpointType: Interface
      VpcId: !Ref: VPC
      ServiceName: !Sub com.amazonaws.${AWS::Region}.logs
      SecurityGroupIds:
        - !Ref InterfaceVpcEndpointSecurityGroup
      SubnetIds:
        - !Ref PublicSubnet01
        - !Ref PublicSubnet02
        - !Ref PublicSubnet03
```