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
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
新しいUbuntu/GNOMEが使いたいからXからWaylandへ移行頑張ってるの巻 2026-06-20
nobutomurata
0
160
入門!AWS Blocks
ysuzuki
1
170
クラウドファンディング版StackChan 3体(4体)をインタラクティブな体験型作品にして展示もした話 / スタックチャンお誕生日会2026
you
PRO
0
140
自宅LLMの話
jacopen
1
700
ACE-Step-1.5で見る 音楽生成AIのしくみと“破綻だけ直す”Retake機能の開発【zennfes spring 2026 登壇資料】
personabb
1
550
SteampipeとExcel Power QueryでAWS構成定義書の作成を自動化する
jhashimoto
0
170
AIネイティブな開発のサプライチェーンリスク対策 〜激動の開発現場でリスクに立ち向かう〜【ZennFes】
cscengineer
PRO
2
150
AIAU_UMEMOGU_ninomiya_slide
ninomiya_ii
0
250
ぼっちではじめた登壇が「51名」「241件」の発信に化けた
subroh0508
1
290
水を運ぶ人としてのリーダーシップ
izumii19
3
680
Bucharest Tech Week 2026 - Reinventing testing practices in the AI era
edeandrea
PRO
1
170
徹底討論!ECS vs EKS!
daitak
3
1.2k
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.9k
What's in a price? How to price your products and services
michaelherold
247
13k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
200
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
My Coaching Mixtape
mlcsv
0
150
Site-Speed That Sticks
csswizardry
13
1.2k
Discover your Explorer Soul
emna__ayadi
2
1.1k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Building Adaptive Systems
keathley
44
3.1k
Done Done
chrislema
186
16k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
600
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の基礎を学ぼう