Skip to content

Instantly share code, notes, and snippets.

@franzwong
franzwong / index.js
Created June 16, 2019 04:49
S3 POST policy example
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
const uuidv4 = require('uuid/v4');
const bucketName = process.env.bucketName;
const kmsKeyId = process.env.kmsKeyId;
const MAX_FILE_SIZE = 5 * 1024 * 1024;
const EXPIRATION = 900;
@franzwong
franzwong / app.component.ts
Created March 6, 2019 14:15
Integrate Google OAuth with Angular 7
import { Component, OnInit, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { environment } from './../environments/environment';
const clientId = environment.clientId;
const scope = 'https://www.googleapis.com/auth/youtube.readonly';
@franzwong
franzwong / stack.tf
Created February 4, 2019 10:12
Sample Terraform stack for EC2 and VPC
variable "aws_region" {
default = "us-east-1"
}
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
variable "webSubnetCidr" {
default = "10.0.1.0/24"
@franzwong
franzwong / main.js
Created January 23, 2019 14:41
HowTo: Grant IAM role to Cognito authenticated user
let response = await login(email, password)
const idToken = response.AuthenticationResult.IdToken
const { identityId, credentials } = await getUserCredentials(idToken)
const s3 = new AWS.S3({ credentials })
response = await s3.putObject({
Body: 'Hello world',
Bucket: process.env.S3_BUCKET,
Key: `${identityId}/foo.txt`,
ContentType: 'text/plain'
@franzwong
franzwong / getUserCredentials.js
Created January 23, 2019 14:29
HowTo: Implement user sign up and login with AWS Cognito
async function getUserCredentials(idToken) {
const cognitoidentity = new AWS.CognitoIdentity()
const providerName = `cognito-idp.${process.env.AWS_REGION}.amazonaws.com/${process.env.USER_POOL_ID}`
let response = await cognitoidentity.getId({
IdentityPoolId: process.env.IDENTITY_POOL_ID,
Logins: {
[providerName]: idToken
}
}).promise()
@franzwong
franzwong / permissionPolicy.json
Last active January 24, 2019 10:16
HowTo: Grant IAM role to Cognito authenticated user
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
@franzwong
franzwong / trustPolicy.json
Last active January 24, 2019 10:17
HowTo: Grant IAM role to Cognito authenticated user
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Federated": "cognito-identity.amazonaws.com"},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {"cognito-identity.amazonaws.com:aud": "<IDENTITY_POOL_ID>"},
"ForAnyValue:StringLike": {"cognito-identity.amazonaws.com:amr": "authenticated"}
}
@franzwong
franzwong / login.js
Created January 22, 2019 12:01
HowTo: Implement user sign up and login with AWS Cognito
async function login(email, password) {
try {
const cognito = new AWS.CognitoIdentityServiceProvider()
return await cognito.adminInitiateAuth({
AuthFlow: 'ADMIN_NO_SRP_AUTH',
ClientId: process.env.CLIENT_ID,
UserPoolId: process.env.USER_POOL_ID,
AuthParameters: {
USERNAME: email,
PASSWORD: password
@franzwong
franzwong / signUp.js
Created January 22, 2019 11:57
HowTo: Implement user sign up and login with AWS Cognito
async function signUp(email, password) {
try {
const cognito = new AWS.CognitoIdentityServiceProvider()
await cognito.adminCreateUser({
UserPoolId: process.env.USER_POOL_ID,
Username: email,
MessageAction: 'SUPPRESS',
TemporaryPassword: password,
}).promise()
@franzwong
franzwong / exchange-rate.yaml
Last active January 12, 2019 11:27
HowTo: Create AWS Lambda with Cloudformation
AWSTemplateFormatVersion: 2010-09-09
Parameters:
AwsRegion:
Type: String
Default: us-east-1
# This bucket stores the lambda function package
LambdaCodeBucket:
Type: String
Default: my-lambda
LambdaCodeKey: