姉さん、New Relic デビューしたのでLambdaと・・・・

# 1.はじめに
こちらの記事で、New Relic デビューをしてから5日が経過しました。

https://qiita.com/horit0123/items/a53013db9d42711b648d

完全に理解した！！って最後に言ってますが、次のBlogを書いていたら、「お、完全に理解したし、ついでにやっとく？」と思いました。

https://qiita.com/horit0123/items/0cc147d4081803d46c14

ということで、New RelicとLambdaを完全に理解するための記事を一本書くことにしました。

# 2. AWS Lambdaアプリケーションの構成
意味は特になし。何も考えず、とりあえず、こんなアプリを作ろうと考えた。とりあえず、正規の流れと、エラー時のDLQがある感じ。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/50524c1e-662c-260c-9ce9-a0545275abb4.png)

これがどう可視化されるのか、見せてもらおうか、New Relicさんよ

# 3. さて、困った。まずは環境を作ろう。
New Relicさんよ、これ、どう料理してくるの？と途方に暮れている。
Lambdaとつなげるだけなら、なんとなくイメージができるが、このリソースだけ連携したいってできるんだろうか。

まずは、上記リソースをSAMで定義。

テンプレートはこちら。
<details><summary>SAMテンプレート</summary><div>

```yaml:template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-lambda-eventfiltering

Globals:
  Function:
    Timeout: 10
Resources:
  HttpAPI:
    Type: AWS::Serverless::HttpApi
    Properties:
      AccessLogSettings:
        DestinationArn: !GetAtt LogGroup.Arn
        Format: '{ "accountId" : "$context.accountId", "requestId" : "$context.requestId", "ip" : "$context.identity.sourceIp", "caller" : "$context.identity.caller", "user" : "$context.identity.user", "requestTime" : "$context.requestTime", "httpMethod" : "$context.httpMethod", "resourcePath" : "$context.routeKey", "path" : "$context.path", "status" : "$context.status", "protocol" : "$context.protocol", "responseLatency" : "$context.responseLatency","IntegrationLatency" : "$context.integrationLatency", "responseLength" : "$context.responseLength" }'
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location:
              openapi.yml
      FailOnWarnings: true

  # Logs
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /apigateway/${AWS::StackName}
      RetentionInDays: 7

  ProducerFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: !Sub ${AWS::StackName}-ProducerFunction
      CodeUri: ProducerFunction/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Environment:
        Variables:
          STREAM_NAME: !Ref  Stream 
      Policies:
          - Version: '2012-10-17' 
            Statement:
              - Effect: Allow
                Action: kinesis:PutRecord
                Resource: 
                  - !GetAtt Stream.Arn
  # Logs
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${AWS::StackName}-ProducerFunction
      RetentionInDays: 7


  Stream: 
    Type: AWS::Kinesis::Stream 
    Properties: 
        Name: !Sub ${AWS::StackName}-Stream 
        RetentionPeriodHours: 168 
        ShardCount: 1

  BackendFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: !Sub ${AWS::StackName}-BackendFunction
      CodeUri: BackendFunction/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Environment:
        Variables:
          QUEUE_URL: !Ref Queue        
      Events:
        KinesisEvent:
          Type: Kinesis
          Properties:
            Stream: !GetAtt Stream.Arn
            StartingPosition: LATEST
            BatchSize: 10
            MaximumBatchingWindowInSeconds: 10
            MaximumRetryAttempts: 2
            DestinationConfig:
                OnFailure:
                  Destination: !GetAtt FunctionDLQ.Arn           
            Enabled: True
            FilterCriteria: 
              Filters: 
                - Pattern: '{"data": {"PRIORITY": [ { "numeric": [ "=", 100 ] } ]}}' 
      Policies:
          - Version: '2012-10-17' 
            Statement:
              - Effect: Allow
                Action: sqs:SendMessage
                Resource: 
                  - !GetAtt FunctionDLQ.Arn
                  - !GetAtt Queue.Arn
  # Logs
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${AWS::StackName}-BackendFunction
      RetentionInDays: 7


  Topic:
    Type: AWS::SNS::Topic
    Properties:
      KmsMasterKeyId: alias/aws/sns
      
  Queue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub  ${AWS::StackName}-Queue
      RedrivePolicy: 
        deadLetterTargetArn: !GetAtt SQSDLQ.Arn
        maxReceiveCount: 3
 
  QueuePolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref Queue
      PolicyDocument:
        Statement:
          - Sid: AllowSNSSendMessage
            Effect: Allow
            Principal: "*"
            Action:
              - sqs:SendMessage
            Resource: !GetAtt Queue.Arn
            Condition:
              ArnEquals:
                aws:SourceArn: !Ref Topic

  SQSDLQ: 
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${AWS::StackName}-SQS-DLQ


  SnsSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: sqs
      Endpoint: !GetAtt Queue.Arn
      Region: !Ref AWS::Region
      TopicArn: !Ref Topic

  EntityFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: !Sub ${AWS::StackName}-EntityFunction
      CodeUri: EntityFunction/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        SQSEvent:
          Type: SQS
          Properties:
            Queue: !GetAtt Queue.Arn
            BatchSize: 10
            MaximumBatchingWindowInSeconds: 10
            Enabled: True
            FilterCriteria: 
              Filters: 
                - Pattern: '{"body": {"PRIORITY": [ { "numeric": [ "<", 101 ] } ]}}' 
  # Logs
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${AWS::StackName}-EntityFunction
      RetentionInDays: 7
                
  FunctionDLQ: 
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${AWS::StackName}-FunctionDLQ
      
      
Outputs:
  HttpApiUrl:
    Description: URL of your API endpoint
    Value:
      Fn::Sub: 'https://${HttpAPI}.execute-api.${AWS::Region}.${AWS::URLSuffix}/'



```

</div></details>

上記をデプロイする際には、[こちら](https://aws.amazon.com/jp/about-aws/whats-new/2021/10/aws-sam-accelerate-test-code-changes-cloud/)で紹介されているAWS SAM Accelerate(Public Preview)を利用してみた。これ **最高**

変更があったところだけをビルドしてくれる。
<details><summary>sam build</summary><div>

```bash:sam
sam build -c --use-container
Starting Build use cache
Starting Build inside a container
Valid cache found, copying previously built resources from function build definition of 41b5f567-f801-4e26-8216-fcd6e91d084f
Valid cache found, copying previously built resources from function build definition of 7e65aa0e-777d-4089-a680-e382ad85b126
Valid cache found, copying previously built resources from function build definition of 35723abb-a2eb-4191-a1d8-a4bc7e224f9b

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided
        
```

</div></details>
そしてこれが最高。ビルドして、デプロイして、さらに変更を検知して自動デプロイしてくれる

<details><summary>sam sync</summary><div>


```bash:sam

sam sync --stack-name sam-newrelic --watch

        Creating the required resources...
        Successfully created!

                Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-19pogohle14me

                Default capabilities applied: ('CAPABILITY_NAMED_IAM', 'CAPABILITY_AUTO_EXPAND')
To override with customized capabilities, use --capabilities flag or set it in samconfig.toml

This feature is currently in beta. Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/.

The SAM CLI will use the AWS Lambda, Amazon API Gateway, and AWS StepFunctions APIs to upload your code without 
performing a CloudFormation deployment. This will cause drift in your CloudFormation stack. 
**The sync command should only be used against a development stack**.

Confirm that you are synchronizing a development stack and want to turn on beta features.

Enter Y to proceed with the command, or enter N to cancel:
 [y/N]: AWSReservedSSO_AdministratorAccess_5f16b3a82a040f87:~/environment/sam-newrelic $  sam sync --stack-name sam-newrelic --watch

                Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-19pogohle14me

                Default capabilities applied: ('CAPABILITY_NAMED_IAM', 'CAPABILITY_AUTO_EXPAND')
To override with customized capabilities, use --capabilities flag or set it in samconfig.toml

This feature is currently in beta. Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/.

The SAM CLI will use the AWS Lambda, Amazon API Gateway, and AWS StepFunctions APIs to upload your code without 
performing a CloudFormation deployment. This will cause drift in your CloudFormation stack. 
**The sync command should only be used against a development stack**.

Confirm that you are synchronizing a development stack and want to turn on beta features.

Enter Y to proceed with the command, or enter N to cancel:
 [y/N]: y

Experimental features are enabled for this session.
Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/.

Queued infra sync. Wating for in progress code syncs to complete...
Starting infra sync.
Manifest is changed for 41b5f567-f801-4e26-8216-fcd6e91d084f, downloading dependencies and copying/building source
Building codeuri: /home/ec2-user/environment/sam-newrelic/ProducerFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['ProducerFunction']
Manifest is changed for 7e65aa0e-777d-4089-a680-e382ad85b126, downloading dependencies and copying/building source
Building codeuri: /home/ec2-user/environment/sam-newrelic/BackendFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['BackendFunction']
Manifest is changed for 35723abb-a2eb-4191-a1d8-a4bc7e224f9b, downloading dependencies and copying/building source
Building codeuri: /home/ec2-user/environment/sam-newrelic/EntityFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['EntityFunction']
Running PythonPipBuilder:CleanUp
Clean up action: .aws-sam/deps/35723abb-a2eb-4191-a1d8-a4bc7e224f9b does not exist and will be skipped.
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CleanUp
Clean up action: .aws-sam/deps/7e65aa0e-777d-4089-a680-e382ad85b126 does not exist and will be skipped.
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CleanUp
Clean up action: .aws-sam/deps/41b5f567-f801-4e26-8216-fcd6e91d084f does not exist and will be skipped.
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/auto-dependency-layer
Built Template   : .aws-sam/auto-dependency-layer/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t .aws-sam/auto-dependency-layer/template.yaml
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided --template-file .aws-sam/auto-dependency-layer/template.yaml
        

Successfully packaged artifacts and wrote output template to file /tmp/tmp7tu7w5lb.
Execute the following command to deploy the packaged template
sam deploy --template-file /tmp/tmp7tu7w5lb --stack-name <YOUR STACK NAME>


        Deploying with following values
        ===============================
        Stack name                   : sam-newrelic
        Region                       : ap-northeast-1
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisourcebucket-abc
        Capabilities                 : ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]
        Parameter overrides          : {}
        Signing Profiles             : null

Initiating deployment
=====================

2021-12-03 02:55:06 - Waiting for stack create/update to complete

CloudFormation events from stack operations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                   ResourceType                                     LogicalResourceId                                ResourceStatusReason                           
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS                               AWS::CloudFormation::Stack                       sam-newrelic                                     Transformation succeeded                       
CREATE_IN_PROGRESS                               AWS::SNS::Topic                                  Topic                                            -                                              
CREATE_IN_PROGRESS                               AWS::SQS::Queue                                  FunctionDLQ                                      -                                              
CREATE_IN_PROGRESS                               AWS::CloudFormation::Stack                       AwsSamAutoDependencyLayerNestedStack             -                                              
CREATE_IN_PROGRESS                               AWS::SQS::Queue                                  SQSDLQ                                           -                                              
CREATE_IN_PROGRESS                               AWS::Logs::LogGroup                              LogGroup                                         -                                              
CREATE_IN_PROGRESS                               AWS::IAM::Role                                   EntityFunctionRole                               -                                              
CREATE_IN_PROGRESS                               AWS::SNS::Topic                                  Topic                                            Resource creation Initiated                    
CREATE_IN_PROGRESS                               AWS::IAM::Role                                   EntityFunctionRole                               Resource creation Initiated                    
CREATE_IN_PROGRESS                               AWS::Kinesis::Stream                             Stream                                           -                                              
CREATE_IN_PROGRESS                               AWS::SQS::Queue                                  FunctionDLQ                                      Resource creation Initiated                    
(省略)                                   -                                              
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CloudFormation outputs from deployed stack
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs                                                                                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Key                 HttpApiUrl                                                                                                                                                                   
Description         URL of your API endpoint                                                                                                                                                     
Value               https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/                                                                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Stack creation succeeded. Sync infra completed.

{'StackId': 'arn:aws:cloudformation:ap-northeast-1:xxxxxx:stack/sam-newrelic/6bb4c260-53e4-11ec-a339-064a048f128d', 'ResponseMetadata': {'RequestId': 'xxxxxx-3414-xxxx-xxxx-2080f7b0907d', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': ''xxxxxx-3414-xxxx-xxxx-2080f7b0907d', 'content-type': 'text/xml', 'content-length': '387', 'date': 'Fri, 03 Dec 2021 02:55:06 GMT'}, 'RetryAttempts': 0}}
Infra sync completed.

```

</div></details>

この後、変更すると自動でデプロイ。すごく便利！でも、**これ、テスト用であり、プロダクション用ではないのでご注意を。！！！**


<details><summary>sam sync 自動更新の様子</summary><div>

```
Infra sync completed.
Queued infra sync. Wating for in progress code syncs to complete...
Starting infra sync.
Manifest is not changed for 41b5f567-f801-4e26-8216-fcd6e91d084f, running incremental build
Building codeuri: /home/ec2-user/environment/sam-newrelic/ProducerFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['ProducerFunction']
Manifest is not changed for 7e65aa0e-777d-4089-a680-e382ad85b126, running incremental build
Building codeuri: /home/ec2-user/environment/sam-newrelic/BackendFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['BackendFunction']
Manifest is not changed for 35723abb-a2eb-4191-a1d8-a4bc7e224f9b, running incremental build
Building codeuri: /home/ec2-user/environment/sam-newrelic/EntityFunction runtime: python3.9 metadata: {} architecture: x86_64 functions: ['EntityFunction']
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource
Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/auto-dependency-layer
Built Template   : .aws-sam/auto-dependency-layer/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t .aws-sam/auto-dependency-layer/template.yaml
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided --template-file .aws-sam/auto-dependency-layer/template.yaml
        

Successfully packaged artifacts and wrote output template to file /tmp/tmpxp_4u4_w.
Execute the following command to deploy the packaged template
sam deploy --template-file /tmp/tmpxp_4u4_w --stack-name <YOUR STACK NAME>


        Deploying with following values
        ===============================
        Stack name                   : sam-newrelic
        Region                       : ap-northeast-1
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisourcebucket-
        Capabilities                 : ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]
        Parameter overrides          : {}
        Signing Profiles             : null

Initiating deployment
=====================

2021-12-03 03:29:32 - Waiting for stack create/update to complete

CloudFormation events from stack operations
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                                                                       ResourceType                                                                                         LogicalResourceId                                                                                    ResourceStatusReason                                                                               
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
^[[6UPDATE_IN_PROGRESS                                                                                   AWS::CloudFormation::Stack                                                                           sam-newrelic                                                                                         Transformation succeeded                                                                           
UPDATE_IN_PROGRESS                                                                                   AWS::CloudFormation::Stack                                                                           AwsSamAutoDependencyLayerNestedStack                                                                 -                                                                                                  
UPDATE_COMPLETE                                                                                      AWS::CloudFormation::Stack                                                                           AwsSamAutoDependencyLayerNestedStack                                                                 -                                                                                                  
UPDATE_IN_PROGRESS                                                                                   AWS::IAM::Role                                                                                       APIIAMRole                                                                                           -                                                                                                  
UPDATE_COMPLETE                                                                                      AWS::IAM::Role                                                                                       APIIAMRole                                                                                           -                                                                                                  
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS                                                                  AWS::CloudFormation::Stack                                                                           sam-newrelic                                                                                         -                                                                                                  
UPDATE_COMPLETE                                                                                      AWS::CloudFormation::Stack                                                                           sam-newrelic                                                                                         -                                                                                                  
UPDATE_COMPLETE                                                                                      AWS::CloudFormation::Stack                                                                           AwsSamAutoDependencyLayerNestedStack                                                                 -                                                                                                  
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CloudFormation outputs from deployed stack
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs                                                                                                                                                                                                                                                                                                                                                                                                           
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Key                 HttpApiUrl                                                                                                                                                                                                                                                                                                                                                                                    
Description         URL of your API endpoint                                                                                                                                                                                                                                                                                                                                                                      
Value               https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/                                                                                                                                                                                                                                                                                                                                  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Stack update succeeded. Sync infra completed.

{'StackId': 'arn:aws:cloudformation:ap-northeast-1:0123456789012:stack/sam-newrelic/6bb4c260-53e4-11ec-a339-064a048f128d', 'ResponseMetadata': {'RequestId': '7a17b289-09c6-413d-b9e4-d69c38f64a29', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '7a17b289-09c6-413d-b9e4-d69c38f64a29', 'content-type': 'text/xml', 'content-length': '387', 'date': 'Fri, 03 Dec 2021 03:29:31 GMT'}, 'RetryAttempts': 0}}
Infra sync completed.

```

</div></details>


よし、Outputに出力されたURLにCurlでアクセス。

```
curl  https://dgz0kn4bnh.execute-api.ap-northeast-1.amazonaws.com/hoge
{"message": "hello world"}
```

お、応答された。全部動いてるかな～。ログを見てみよう。

```
 sam logs --stack-name sam-newrelic --tail --beta-features

2021/12/03/[$LATEST]bbaca989cc4243d59493ccb5a80c19be 2021-12-03T03:47:15.927000 START RequestId: 657e7e13-cbdd-41fd-96e8-0bb5366f0356 Version: $LATEST
2021/12/03/[$LATEST]bbaca989cc4243d59493ccb5a80c19be 2021-12-03T03:47:15.930000 {'EVENT_TIME': '2021-12-03T03:47:15.930066', 'ID': 'ID-842574', 'PRICE': 23755, 'PRIORITY': 2}
2021/12/03/[$LATEST]bbaca989cc4243d59493ccb5a80c19be 2021-12-03T03:47:16.110000 END RequestId: 657e7e13-cbdd-41fd-96e8-0bb5366f0356
2021/12/03/[$LATEST]bbaca989cc4243d59493ccb5a80c19be 2021-12-03T03:47:16.110000 REPORT RequestId: 657e7e13-cbdd-41fd-96e8-0bb5366f0356  Duration: 180.69 ms     Billed Duration: 181 ms Memory Size: 128 MB     Max Memory Used: 65 MB
2021/12/03/[$LATEST]704ca1c4d7a9447e9f0b83d6da7dcf2e 2021-12-03T03:47:17.472000 START RequestId: 20c111d8-ca64-4a42-9c03-564c2639cb04 Version: $LATEST
2021/12/03/[$LATEST]704ca1c4d7a9447e9f0b83d6da7dcf2e 2021-12-03T03:47:17.475000 Decoded payload: {"EVENT_TIME": "2021-12-03T03:47:15.930066", "ID": "ID-842574", "PRICE": 23755, "PRIORITY": 100}
2021/12/03/[$LATEST]704ca1c4d7a9447e9f0b83d6da7dcf2e 2021-12-03T03:47:17.642000 END RequestId: 20c111d8-ca64-4a42-9c03-564c2639cb04
2021/12/03/[$LATEST]704ca1c4d7a9447e9f0b83d6da7dcf2e 2021-12-03T03:47:17.642000 REPORT RequestId: 20c111d8-ca64-4a42-9c03-564c2639cb04  Duration: 167.21 ms     Billed Duration: 168 ms Memory Size: 128 MB     Max Memory Used: 65 MB
dgz0kn4bnh_.default-2021-12-03-03-47 2021-12-03T03:47:15.898000 {
  "accountId": "0123345678912",
  "requestId": "JwPyogbrNjMEMQg=",
  "ip": "0.0.0.0",
  "caller": "-",
  "user": "-",
  "requestTime": "03/Dec/2021:03:47:15 +0000",
  "httpMethod": "GET",
  "resourcePath": "ANY /{proxy+}",
  "path": "/hoge",
  "status": "200",
  "protocol": "HTTP/1.1",
  "responseLatency": "194",
  "IntegrationLatency": "192",
  "responseLength": "26"
}
2021/12/03/[$LATEST]1f8a453df389493eb18366032fa2e637 2021-12-03T03:47:37.701000 START RequestId: ed6ada3e-edd6-595d-8a89-801d785435d9 Version: $LATEST
2021/12/03/[$LATEST]1f8a453df389493eb18366032fa2e637 2021-12-03T03:47:37.703000 {'Records': [{'messageId': '4e4da240-c5e7-4df3-a561-473593f515d5', 'receiptHandle': 'AQEB5rl7O9zr6YWajKAgsWAkeZP+NszWV/qwGf9UfsKYRLtyq076SRuLMv2FnfFy0D8Aq6fGcUgDdNjBooQzJ5y/k8NXequRCAGubm0Cgmk8vq4SBwyaRSY83ndwtLkrG8wsrDtr534NTxuI45yu3+bmB+uGcOzMAxTlwBKfrw/mTC4Zb4CcRcXDBgYa7i7M2tu1on0NZi93guBeRNaOLZ90H4mw==', 'body': '{"EVENT_TIME": "2021-12-03T03:47:15.930066", "ID": "ID-842574", "PRICE": 23755, "PRIORITY": 100}', 'attributes': {'ApproximateReceiveCount': '1', 'SentTimestamp': '1638503237633', 'SenderId': 'sam-newrelic-BackendFunction', 'ApproximateFirstReceiveTimestamp': '1638503237639'}, 'messageAttributes': {}, 'md5OfMessageAttributes': None, 'md5OfBody': 'bcc4264ef0de3380b966cf933cd892d0', 'eventSource': 'aws:sqs', 'eventSourceARN': 'arn:aws:sqs:ap-northeast-1:12345689012:sam-newrelic-Queue', 'awsRegion': 'ap-northeast-1'}]}
2021/12/03/[$LATEST]1f8a453df389493eb18366032fa2e637 2021-12-03T03:47:37.704000 END RequestId: ed6ada3e-edd6-595d-8a89-801d785435d9
2021/12/03/[$LATEST]1f8a453df389493eb18366032fa2e637 2021-12-03T03:47:37.704000 REPORT RequestId: ed6ada3e-edd6-595d-8a89-801d785435d9  Duration: 1.06 ms       Billed Duration: 2 ms   Memory Size: 128 MB     Max Memory Used: 37 MB

```

# 3. よし、つなげるぞ、New Relicさん。
こちらに手順がある。まずこれやるか。


https://docs.newrelic.com/jp/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/enable-aws-lambda-monitoring/

その[Step 1: Link your AWS and New Relic accounts](https://docs.newrelic.com/jp/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/account-linking/)

#### CLI導入
今回用に新規に立てた、Cloud9を使っているから、権限、CLI,Pythonは大丈夫だから、まずは、これ。うむ簡単。
```
pip3 install newrelic-lambda-cli
```
#### API Key作成&入手

次は？というとAPI Key。[これ](https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher)で入手する画面に移動。新規にユーザーのキーを作成し入手。NameはAWSLambdaで作ってみました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/b248deed-34a4-4f79-7c22-c559c27b0be1.png)



次は・・・[Step 1: Link your AWS and New Relic accounts](https://docs.newrelic.com/jp/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/account-linking/)


#### 設定

```bash
newrelic-lambda integrations install --nr-account-id YOUR_NR_ACCOUNT_ID \
    --nr-api-key YOUR_NEW_RELIC_USER_KEY

Validating New Relic credentials
Retrieving integration license key
Creating the AWS role for the New Relic AWS Lambda Integration
Waiting for stack creation to complete... ✔️ Done
✔️ Created role [NewRelicLambdaIntegrationRole_XXXXXX] in AWS account.
Linking New Relic account to AWS account
✔️ Cloud integrations account [New Relic AWS Integration - 0123456789012] was created in New Relic account [XXXXXX] with IAM role [arn:aws:iam::0123456789012:role/NewRelicLambdaIntegrationRole_XXXXXX].
Enabling Lambda integration on the link between New Relic and AWS
✔️ Integration [id=1093110, name=Lambda] has been enabled in Cloud integrations account [New Relic AWS Integration - 0123456789012] of New Relic account [XXXXXX].
Creating the managed secret for the New Relic License Key
Setting up NewRelicLicenseKeySecret stack in region: ap-northeast-1
Creating change set: NewRelicLicenseKeySecret-CREATE-xxxxx
Waiting for change set creation to complete, this may take a minute... Waiting for change set to finish execution. This may take a minute... ✔️ Done
Creating newrelic-log-ingestion Lambda function in AWS account
Setting up 'newrelic-log-ingestion' function in region: ap-northeast-1
Fetching new CloudFormation template url
Creating change set: NewRelicLogIngestion-CREATE-xxxxx
Waiting for change set creation to complete, this may take a minute... Waiting for change set to finish execution. This may take a minute... ✔️ Done
✨ Install Complete ✨
```
待っていたら終わった。スタックが3つ作られている。。。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/1b52c42d-9fad-1b34-d30d-97f2445b8016.png)


ここまで終わると、次はこれだな。Step2はオプションだし、3は学ぶためっぽいから。
[Step 4: Instrument your own Lambda functions](https://docs.newrelic.com/jp/docs/serverless-function-monitoring/aws-lambda-monitoring/enable-lambda-monitoring/instrument-your-own/)

私は、SAMテンプートが既にあるから、そこに追加しよう。以下の部分を追加したら、デプロイ！

- 環境変数
- Layer
- ポリシー

```
      Environment:
        Variables:
          # For the instrumentation handler to invoke your real handler, we need this value
          NEW_RELIC_LAMBDA_HANDLER: app.lambdaHandler
          # Distributed tracing needs your account ID, and your trusted account ID
          NEW_RELIC_ACCOUNT_ID: YOUR_ACCOUNT_ID_HERE
          # If your New Relic account has a parent account, this value should be that account ID. Otherwise, just
          # your account id.
          NEW_RELIC_TRUSTED_ACCOUNT_KEY: YOUR_PARENT_ACCOUNT_ID_HERE
      Layers:
        # This layer includes the New Relic Lambda Extension, a sidecar process that sends telemetry,
        # as well as the New Relic Agent for Node.js, and a handler wrapper that makes integration easy.
        - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:451483290750:layer:NewRelicNodeJS12X:34
      Policies:
        # This policy allows the lambda to know the value of the New Relic licence key. We need this so
        # that we can send telemetry back to New Relic
        - AWSSecretsManagerGetSecretValuePolicy:
            SecretArn: !ImportValue NewRelicLicenseKeySecret-NewRelic-LicenseKeySecretARN
```

動いた！

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/51adf5a9-b213-c810-2d87-a5acf4580d0e.png)

画面の右側には、AWSマネジメントコンソールへのリンクもあり、簡単にアクセスが可能。

#### 設定その２

よし、次はX-Rayだ！この手順で、わずか数クリックで接続が可能。

https://docs.newrelic.com/jp/docs/infrastructure/amazon-integrations/aws-integrations-list/aws-x-ray-monitoring-integration/

でも、これだけじゃ、実はダメ。私のテンプレートではLambda 関数の設定としてTracingがActiveになっていないこと、X-Rayを利用する権限がないことから、この両方を追加しデプロイしたら見事、連携されました。

```yaml:Lambda環境変数
      Environment:
        Variables:
          # For the instrumentation handler to invoke your real handler, we need this value
          NEW_RELIC_LAMBDA_HANDLER: app.lambda_handler
          # Distributed tracing needs your account ID, and your trusted account ID
          NEW_RELIC_ACCOUNT_ID: xxxxxx
          # If your New Relic account has a parent account, this value should be that account ID. Otherwise, just
          # your account id.
          NEW_RELIC_TRUSTED_ACCOUNT_KEY: xxxxxx


```

```yaml:全関数に共通で付与するためGlobalsにTracingを追加
Globals:
  Function:
    Timeout: 20
    Tracing: Active
```

LayerはNew Relicさんが提供してくれるものをそのまま利用してます。

```yaml:Layer

      Layers:
        # This layer includes the New Relic Lambda Extension, a sidecar process that sends telemetry,
        # as well as the New Relic Agent for Node.js, and a handler wrapper that makes integration easy.
        - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:451483290750:layer:NewRelicNodeJS12X:34
```

##結果
###関数の全体
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/4030f5f0-0c45-db88-5d72-42e3a3a83739.png)


###関数内の詳細

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/46d08fa8-c921-1e4b-865d-dcabababd6f4.png)

よし、SNS/SQS/Kinesisは？というと、X-Rayと同じだ。数クリックで設定完了。

https://docs.newrelic.com/jp/docs/infrastructure/amazon-integrations/aws-integrations-list/amazon-sqs-monitoring-integration/

https://docs.newrelic.com/jp/docs/infrastructure/amazon-integrations/aws-integrations-list/aws-sns-monitoring-integration/

https://docs.newrelic.com/jp/docs/infrastructure/amazon-integrations/aws-integrations-list/aws-kinesis-data-streams-monitoring-integration/

ということで、Kinesis /SQS/ SNSも無事、New Relicに連携しました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/8629adba-374a-4f3d-2c1f-522553e7c7eb.png)


キューのメッセージの状況も可視化されてますね。


#まとめ

New Relicを使ったことが無いとは言えないとおもって使い始めました。アカウントのセットアップからLambdaとの連携まで試してみましたが、特に困ることなくセットアップできました。
ただ、使いこなせたか？というと、単につないだだけなので、もっといろいろ面白い世界があるはず・・・。
また、そのうち試してみたいと思います。

そして、API Gatewayに毎秒アクセスするcurlコマンドを出し続けるとこうなります。お気を付けを。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/535037/2bc77694-65d0-0ad3-0b1b-812f4792aad0.png)