Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active June 13, 2017 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/1f6aaf9134e8da8d2f17c56485d76c8f to your computer and use it in GitHub Desktop.
Save wokamoto/1f6aaf9134e8da8d2f17c56485d76c8f to your computer and use it in GitHub Desktop.
[stripe][AWS] Stripe の Event を CloudWatch custom metrics に記録 ref: http://qiita.com/wokamoto/items/060b7cc4e458f4bba4d3
$ npm install stripe
$ serverless deploy -v
:
Serverless: Stack update finished...
Service Information
service: put-stripe-event-to-custom-metrics
stage: test
region: us-east-1
api keys:
None
endpoints:
POST - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/test/stripe/incoming
functions:
incoming: put-stripe-event-to-custom-metrics-test-incoming
Stack Outputs
ServiceEndpoint: https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/test
ServerlessDeploymentBucketName: put-stripe-event-to-cust-serverlessdeploymentbuck-xxxxxxxxxxxx
IncomingLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:000000000000:function:put-stripe-event-to-custom-metrics-test-incoming:1
$ serverless deploy -v -s live
'use strict';
module.exports.incoming = (event, context, callback) => {
const stripe = require('stripe')(process.env.STRIPE_API_KEY); // eslint-disable-line
let response = {
statusCode: 200,
body: ''
};
try {
// Parse Stripe Event
const jsonData = JSON.parse(event.body); // https://stripe.com/docs/api#event_object
// Verify the event by fetching it from Stripe
console.log("Stripe Event: %j", jsonData); // eslint-disable-line
stripe.events.retrieve(jsonData.id, (err, stripeEvent) => {
const AWS = require('aws-sdk');
const cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
const eventType = stripeEvent.type ? stripeEvent.type : '';
const params = {
Namespace: process.env.NAMESPACE,
MetricData: [
{
MetricName: eventType,
Dimensions: [{
Name: process.env.DIMENSION,
Value: process.env.STAGE
}],
Timestamp: new Date,
Unit: 'Count',
Value: '1'
}
]
};
cloudwatch.putMetricData(params, (err) => {
if (err) {
console.log('PutMetricData Error: %j', err); // eslint-disable-line
}
response.body = JSON.stringify({
message: 'Stripe webhook incoming!',
stage: process.env.STAGE,
eventType: eventType
});
return callback(null,response);
});
});
} catch (err) {
response.statusCode = 501;
response.body = JSON.stringify(err);
callback(err,response);
}
};
service: put-stripe-event-to-custom-metrics
custom:
stage: ${opt:stage, self:provider.stage}
logRetentionInDays:
test: "14" # stage[test]は14日
live: "90" # stage[live]は90日
default: "3" # stageが見つからなかったらこれにfallbackするために設定
stripeAPIKey:
test: "__STRIPE_TEST_API_SECRETKEY_HERE___" # stage[test]用の Stripe API Key
live: "__STRIPE_LIVE_API_SECRETKEY_HERE___" # stage[live]用の Stripe API Key
default: "__STRIPE_TEST_API_SECRETKEY_HERE___" # stageが見つからなかったらこれにfallbackするために設定
provider:
name: aws
runtime: nodejs6.10
stage: test
region: us-east-1
memorySize: 128
timeout: 60
iamRoleStatements:
- Effect: Allow
Action:
- cloudwatch:PutMetricData
Resource: "*"
environment:
STAGE: ${self:custom.stage}
# you can add packaging information here
package:
include:
- node_modules/**
exclude:
- .git/**
- package.json
functions:
incoming:
handler: handler.incoming
events:
- http:
path: stripe/incoming
method: post
environment:
NAMESPACE: "___SERVICENAME__HERE__"
DIMENSION: "Stripe"
STRIPE_API_KEY: ${self:custom.stripeAPIKey.${self:custom.stage}, self:custom.stripeAPIKey.default}
resources:
Resources:
IncomingLogGroup:
Properties:
RetentionInDays: ${self:custom.logRetentionInDays.${self:custom.stage}, self:custom.logRetentionInDays.default}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment