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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Hidetaka Okamoto (Stripe)
February 26, 2022
Technology
240
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
560
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
370
AWSでローコードにはじめる サブスクリプション運用の効率化/aws-startup-tech-meetup-fukuoka-2
stripehideokamoto
0
200
Other Decks in Technology
See All in Technology
20260608_Codexの可能性_ノンプログラマー向け_大城追記
doradora09
PRO
0
750
信頼できるテスティングAIをどう育てるか?
odan611
0
190
Redmine 7.0 新機能・機能強化解説(OSC2026京都ダイジェスト版)
vividtone
1
170
システム監視を 「システムを監視するだけ」で 終わらせないために
seiud
0
170
13年運用タイトルのサーバーサイドが辿り着いた現在地 ― モンスターストライクにおける技術・組織・AI活用から得た知見
mixi_engineers
PRO
1
410
コンポーネント名には何を含めるべきなのか? / what-should-be-included-in-component-names
airrnot1106
0
250
CloudWatchから始めるAWS監視
butadora
0
310
OSPN.JPバージョンアップ作業進捗のご報告 / 20260801-osc26kyoto
akkiesoft
0
180
PLaMo 3.0 Primeの事後学習
pfn
PRO
0
260
Claude Code並行開発環境の ムダ‧ムラ‧ムリを見直した話
muranakaaa
2
420
認知負荷をGemini で溶かす — GKE 基盤「Orbit」における AI エージェントの実践
sansantech
PRO
1
210
組織にどうSREを根付かせるか?〜IVRyの場合〜
abnoumaru
0
290
Featured
See All Featured
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
680
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
480
Agile that works and the tools we love
rasmusluckow
331
22k
Navigating Team Friction
lara
192
16k
The Limits of Empathy - UXLibs8
cassininazir
1
580
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
880
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
A better future with KSS
kneath
240
18k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
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の基礎を学ぼう