Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Stripeを組み込む際の CDK実装Tips/awsbasic-202202
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Hidetaka Okamoto (Stripe)
February 26, 2022
Technology
230
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Stripeを組み込む際の CDK実装Tips/awsbasic-202202
AWS エバンジェリストシリーズ AWSの基礎を学ぼうの登壇資料です。
https://awsbasics.connpass.com/event/235164/
Hidetaka Okamoto (Stripe)
February 26, 2022
More Decks by Hidetaka Okamoto (Stripe)
See All by Hidetaka Okamoto (Stripe)
Stripeではじめる Revenue Operations / jp_stripes_okinawa_8
stripehideokamoto
0
330
収益を高めるための Stripeダッシュボード活用術 / jp_stripes_online_7
stripehideokamoto
0
420
No-code SaaS and Full Site Editing / wcasia2023
stripehideokamoto
0
280
コア機能開発に集中するための Stripe Checkout活用法 / jp_stripes_deep_dive_202301
stripehideokamoto
0
550
State of Checkout 2022から見た オンライン決済フローの最適化/jp-stripes-miyzaki-202301
stripehideokamoto
0
240
サブスクリプション決済入門 Stripeでの実装方法と、要件定義時のポイント/okta-stripe-202301
stripehideokamoto
0
220
ノーコード・ローコードツールで サブスク運用を効率化する方法 / JP_Stripes_Osaka_202301
stripehideokamoto
0
170
ビジネスユーザを巻込む Stripeダッシュボード活用法 / jp_stripes_deepdive-202212
stripehideokamoto
0
360
AWSでローコードにはじめる サブスクリプション運用の効率化/aws-startup-tech-meetup-fukuoka-2
stripehideokamoto
0
190
Other Decks in Technology
See All in Technology
FPC(フレキシブル)基板にZephyr実装してみた。
iotengineer22
0
150
AIが自律的に回る開発ループを設計してチーム開発に組み込む
nekorush14
0
110
SONiCの統計情報を取得したい
sonic
0
270
自分が詳しくない領域でAIを使う #プロヒス2026
konifar
20
6.9k
サイバーエージェントにおけるAI推進戦略と変革への取り組み
shotatsuge
0
370
AI-DLCを “そのまま導入しなかった”話 ~組織に合わせてアジャストした 私たちの実践共有~
hiroramos4
PRO
1
370
インシデントレスポンス演習 I / Incident Response Exercise I
ks91
PRO
0
110
20260619 私の日常業務での生成 AI 活用
masaruogura
1
240
Agile and AI Redmine Japan 2026
hiranabe
3
420
SONiCで構築・運用する生成AI向けパブリッククラウドネットワーク ~実装編~
sonic
0
320
SONiCのLinuxベースを活かしたZabbix監視
sonic
0
260
Lightning近況報告
kozy4324
0
220
Featured
See All Featured
The Limits of Empathy - UXLibs8
cassininazir
1
360
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
420
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
490
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Building an army of robots
kneath
306
46k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
160
First, design no harm
axbom
PRO
2
1.2k
How to build a perfect <img>
jonoalderson
1
5.7k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
440
Transcript
Stripeを組み込む際の CDK実装Tips AWS エバンジェリストシリーズ AWSの基礎を学ぼう Hidetaka Okamoto(@hide__dev) 2022/02/26
岡本 秀高 ( @hide__dev ) • Stripe Developer Advocate (ex-developer
in Digitalcube) • JavaScript / TypeScript developer • AWS / Next.js / WordPress / etc… • WordCamp Kyoto 2017 / JP_Stripes Connect 2019 / AWS Samurai 2017 / etc… • 🐈(猫フラご容赦) 2 ジャムジャム!!Jamstack_5 #awsbasics
3 https://jawsug.connpass.com/event/240422/
4 https://stripe.com/jp
サーバーサイドSDKで、簡単に決済系の組み込みが可能 5 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics const session = await
stripe.checkout.sessions.create({ mode: "payment", success_url: `${req.headers.origin}`, cancel_url: `${req.headers.origin}`, line_items:[{ price: req.body.price_id, quantity: 1 }] }) return session.url
6 そのコード、どこにどうやってデプロイしよう? AWS エバンジェリストシリーズ AWSの基礎を学ぼう
7 https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html
AWS CDKでServerless Functionを実装 8 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics # アプリをセットアップ
% mkdir new-cdk-app % cd new-cdk-app % cdk init –language typescript # ライブラリのインストールやファイルの配置 % npm install stripe % mkdir -p resources/functions/ % touch resources/functions/stripe.js # デプロイ % cdk deploy –watch
料金データを返すLambdaの例 9 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics const stripe = require('stripe')(process.env.STRIPE_SECRET_API_KEY);
exports.main = async function(event, context) { try { const { data } = await stripe.prices.list({ lookup_keys: [process.env.STRIPE_PRICE_LOOKUP_KEY] }) return { statusCode: 200, body: JSON.stringify(data) }; } catch(error) { const body = error.stack || JSON.stringify(error, null, 2); return { statusCode: 400, body: JSON.stringify(body) }; } }
LambdaをCDKで定義 10 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics import { Stack, StackProps
} from 'aws-cdk-lib'; import { Construct } from "constructs"; import * as lambda from "aws-cdk-lib/aws-lambda"; export class AwsCdkStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const handler = new lambda.Function(this, "StripeHandler", { runtime: lambda.Runtime.NODEJS_14_X, // So we can use async in widget.js code: lambda.Code.fromAsset("resources/functions"), handler: "stripe.main", environment: { STRIPE_PRICE_LOOKUP_KEY: ‘lookup-key’, STRIPE_SECRET_API_KEY: 'sk_xxx' } }); }}
環境変数を何処に置こう? 11 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics import { Stack, StackProps
} from 'aws-cdk-lib'; import { Construct } from "constructs"; import * as lambda from "aws-cdk-lib/aws-lambda"; export class AwsCdkStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const handler = new lambda.Function(this, "StripeHandler", { runtime: lambda.Runtime.NODEJS_14_X, // So we can use async in widget.js code: lambda.Code.fromAsset("resources/functions"), handler: "stripe.main", environment: { STRIPE_PRICE_LOOKUP_KEY: ‘lookup-key’, STRIPE_SECRET_API_KEY: 'sk_xxx' } }); } }
12 Secrets Managerの値を読み取る AWS エバンジェリストシリーズ AWSの基礎を学ぼう
Secrets Managerの値を環境変数に渡す例 13 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics import * as
sm from "aws-cdk-lib/aws-secretsmanager"; export class AwsCdkStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const secret = sm.Secret.fromSecretAttributes(this, "ImportedSecret", { secretCompleteArn: "登録したSecretのARN" }); const handler = new lambda.Function(this, "StripeHandler", { environment: { STRIPE_PRICE_LOOKUP_KEY: secret.secretValueFromJson('lookup-key').toString(), STRIPE_SECRET_API_KEY: ‘sk_xxx’ } …
14 もっと安全に管理したい AWS エバンジェリストシリーズ AWSの基礎を学ぼう
LambdaにSecrets Managerへのアクセス権限を設定 15 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics const handler =
new lambda.Function(this, "StripeHandler", { runtime: lambda.Runtime.NODEJS_14_X, // So we can use async in widget.js code: lambda.Code.fromAsset("resources/functions"), handler: "stripe.main", environment: { STRIPE_PRICE_LOOKUP_KEY: secret.secretValueFromJson('lookup-key').toString(), } }); handler.addToRolePolicy(new iam.PolicyStatement({ resources: [secret.secretArn], actions: ['secretsManager:GetSecretValue'], }))
Secrets Managerの値を取得して、利用する 16 AWS エバンジェリストシリーズ AWSの基礎を学ぼう #awsbasics const AWS =
require('aws-sdk'); const Stripe = require('stripe'); exports.main = async function(event, context) { try { const secretsmanager = new AWS.SecretsManager(); const secrets = await secretsmanager.getSecretValue({ SecretId: 'secret-id' }) const secretsJSON = JSON.parse(secrets.SecretString) const stripe = new Stripe(secretsJSON.STRIPE_SECRET_API_KEY)
まとめ • AWS CDKを利用すると、アプリの実装言語でAWSインフラも定義できる • StripeなどのAPIキーを利用するSaaSを使う場合、 Secrets Manager / Systems
ManagerとCDKを組み合わせて、 テンプレートからキー情報を秘匿できる • テスト環境・本番環境の切り替えまでやりたい場合は、 CDKのContextを活用しよう https://docs.aws.amazon.com/cdk/v2/guide/get_context_var.html 17 AWS エバンジェリストシリーズ AWSの基礎を学ぼう
Thanks! • Twitterハッシュタグでシェア #awsbasics #JP_Stripes • Twitterアカウント: @hide__dev @StripeJapan •
QiitaでStripeに関する 開発ブログ更新中 https://qiita.com/organizations/stripe 18 AWS エバンジェリストシリーズ AWSの基礎を学ぼう