© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Julio M. Faerman, Developer Relations
April 2017
AWS on Javascript
From the browser to IoT, Edge, Backend and more!
Browser & Mobile
var albumBucketName = 'BUCKET_NAME';
var bucketRegion = 'REGION';
var IdentityPoolId = 'IDENTITY_POOL_ID';
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: albumBucketName}
});
var albumBucketName = 'BUCKET_NAME';
var bucketRegion = 'REGION';
var IdentityPoolId = 'IDENTITY_POOL_ID';
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: albumBucketName}
});
Amazon Cognito
// When using loose Javascript files:
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
// Under the original name:
var CognitoUserPool =
AWSCognito.CognitoIndentityServiceProvider.CognitoUserPool;
// Modules, e.g. Webpack:
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
// ES Modules, e.g. transpiling with Babel
import { CognitoUserPool, CognitoUserAttribute, CognitoUser } from
'amazon-cognito-identity-js';
var poolData = {
UserPoolId : '...', // Your user pool id here
ClientId : '...' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = { Name : 'email', Value : 'email@mydomain.com'};
var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ };
var attributeEmail = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
});
Use case 1. Registering a user with the application.
var poolData = {
UserPoolId : '...', // Your user pool id here
ClientId : '...' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = { Name : 'email', Value : 'email@mydomain.com'};
var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ };
var attributeEmail = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
});
var poolData = {
UserPoolId : '...', // Your user pool id here
ClientId : '...' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = { Name : 'email', Value : 'email@mydomain.com'};
var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ };
var attributeEmail = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
});
24 use cases, including confirmation (SMS & e-mail) and Multi-Factor Authentication
var albumBucketName = 'BUCKET_NAME';
var bucketRegion = 'REGION';
var IdentityPoolId = 'IDENTITY_POOL_ID';
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: albumBucketName}
});
function addPhoto(albumName) {
var files = document.getElementById('photoupload').files;
if (!files.length) {
return alert('Please choose a file to upload first.');
}
var file = files[0];
var fileName = file.name;
var albumPhotosKey = encodeURIComponent(albumName) + '//';
var photoKey = albumPhotosKey + fileName;
s3.upload({
Key: photoKey,
Body: file,
ACL: 'public-read'
}, function(err, data) {
if (err) {
return alert('There was an error uploading your photo: ', err.message);
}
alert('Successfully uploaded photo.');
viewAlbum(albumName);
});
}
Backend
Internet of Things
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
region: <YourAWSRegion>
});
device.on('connect', function() {
console.log('connect');
device.subscribe('topic_1');
device.publish('topic_2', JSON.stringify({ test_data: 1}));
});
device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
region: <YourAWSRegion>
});
device.on('connect', function() {
console.log('connect');
device.subscribe('topic_1');
device.publish('topic_2', JSON.stringify({ test_data: 1}));
});
device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
region: <YourAWSRegion>
});
device.on('connect', function() {
console.log('connect');
device.subscribe('topic_1');
device.publish('topic_2', JSON.stringify({ test_data: 1}));
});
device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
var awsIot = require('aws-iot-device-sdk');
var thingShadows = awsIot.thingShadow({...});
…
thingShadows.on('connect', function() {
thingShadows.register( 'RGBLedLamp', function() {
var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}};
clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState );
});
thingShadows.on('status',
function(thingName, stat, clientToken, stateObject) {});
thingShadows.on('delta',
function(thingName, stateObject) {});
thingShadows.on('timeout',
function(thingName, clientToken) {});
var awsIot = require('aws-iot-device-sdk');
var thingShadows = awsIot.thingShadow({...});
…
thingShadows.on('connect', function() {
thingShadows.register( 'RGBLedLamp', function() {
var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}};
clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState );
});
thingShadows.on('status',
function(thingName, stat, clientToken, stateObject) {});
thingShadows.on('delta',
function(thingName, stateObject) {});
thingShadows.on('timeout',
function(thingName, clientToken) {});
var awsIot = require('aws-iot-device-sdk');
var thingShadows = awsIot.thingShadow({...});
…
thingShadows.on('connect', function() {
thingShadows.register( 'RGBLedLamp', function() {
var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}};
clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState );
});
thingShadows.on('status',
function(thingName, stat, clientToken, stateObject) {});
thingShadows.on('delta',
function(thingName, stateObject) {});
thingShadows.on('timeout',
function(thingName, clientToken) {});
AWS Greengrass
Network Edge
Global Network of Regions and Edge Locations
고맙습니다
@jmfaerman
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017

AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017

  • 1.
    © 2016, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Julio M. Faerman, Developer Relations April 2017 AWS on Javascript From the browser to IoT, Edge, Backend and more!
  • 5.
  • 7.
    var albumBucketName ='BUCKET_NAME'; var bucketRegion = 'REGION'; var IdentityPoolId = 'IDENTITY_POOL_ID'; AWS.config.update({ region: bucketRegion, credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: IdentityPoolId }) }); var s3 = new AWS.S3({ apiVersion: '2006-03-01', params: {Bucket: albumBucketName} });
  • 8.
    var albumBucketName ='BUCKET_NAME'; var bucketRegion = 'REGION'; var IdentityPoolId = 'IDENTITY_POOL_ID'; AWS.config.update({ region: bucketRegion, credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: IdentityPoolId }) }); var s3 = new AWS.S3({ apiVersion: '2006-03-01', params: {Bucket: albumBucketName} });
  • 10.
  • 12.
    // When usingloose Javascript files: var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool; // Under the original name: var CognitoUserPool = AWSCognito.CognitoIndentityServiceProvider.CognitoUserPool; // Modules, e.g. Webpack: var AmazonCognitoIdentity = require('amazon-cognito-identity-js'); var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool; // ES Modules, e.g. transpiling with Babel import { CognitoUserPool, CognitoUserAttribute, CognitoUser } from 'amazon-cognito-identity-js';
  • 13.
    var poolData ={ UserPoolId : '...', // Your user pool id here ClientId : '...' // Your client id here }; var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var attributeList = []; var dataEmail = { Name : 'email', Value : 'email@mydomain.com'}; var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ }; var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail); var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber); attributeList.push(attributeEmail); attributeList.push(attributePhoneNumber); userPool.signUp('username', 'password', attributeList, null, function(err, result){ if (err) { alert(err); return; } cognitoUser = result.user; console.log('user name is ' + cognitoUser.getUsername()); }); Use case 1. Registering a user with the application.
  • 14.
    var poolData ={ UserPoolId : '...', // Your user pool id here ClientId : '...' // Your client id here }; var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var attributeList = []; var dataEmail = { Name : 'email', Value : 'email@mydomain.com'}; var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ }; var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail); var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber); attributeList.push(attributeEmail); attributeList.push(attributePhoneNumber); userPool.signUp('username', 'password', attributeList, null, function(err, result){ if (err) { alert(err); return; } cognitoUser = result.user; console.log('user name is ' + cognitoUser.getUsername()); });
  • 15.
    var poolData ={ UserPoolId : '...', // Your user pool id here ClientId : '...' // Your client id here }; var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var attributeList = []; var dataEmail = { Name : 'email', Value : 'email@mydomain.com'}; var dataPhoneNumber = { Name : 'phone_number', Value : '+15555555555’ }; var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail); var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber); attributeList.push(attributeEmail); attributeList.push(attributePhoneNumber); userPool.signUp('username', 'password', attributeList, null, function(err, result){ if (err) { alert(err); return; } cognitoUser = result.user; console.log('user name is ' + cognitoUser.getUsername()); }); 24 use cases, including confirmation (SMS & e-mail) and Multi-Factor Authentication
  • 16.
    var albumBucketName ='BUCKET_NAME'; var bucketRegion = 'REGION'; var IdentityPoolId = 'IDENTITY_POOL_ID'; AWS.config.update({ region: bucketRegion, credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: IdentityPoolId }) }); var s3 = new AWS.S3({ apiVersion: '2006-03-01', params: {Bucket: albumBucketName} });
  • 18.
    function addPhoto(albumName) { varfiles = document.getElementById('photoupload').files; if (!files.length) { return alert('Please choose a file to upload first.'); } var file = files[0]; var fileName = file.name; var albumPhotosKey = encodeURIComponent(albumName) + '//'; var photoKey = albumPhotosKey + fileName; s3.upload({ Key: photoKey, Body: file, ACL: 'public-read' }, function(err, data) { if (err) { return alert('There was an error uploading your photo: ', err.message); } alert('Successfully uploaded photo.'); viewAlbum(albumName); }); }
  • 19.
  • 24.
  • 26.
    var awsIot =require('aws-iot-device-sdk'); var device = awsIot.device({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, region: <YourAWSRegion> }); device.on('connect', function() { console.log('connect'); device.subscribe('topic_1'); device.publish('topic_2', JSON.stringify({ test_data: 1})); }); device.on('message', function(topic, payload) { console.log('message', topic, payload.toString()); });
  • 27.
    var awsIot =require('aws-iot-device-sdk'); var device = awsIot.device({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, region: <YourAWSRegion> }); device.on('connect', function() { console.log('connect'); device.subscribe('topic_1'); device.publish('topic_2', JSON.stringify({ test_data: 1})); }); device.on('message', function(topic, payload) { console.log('message', topic, payload.toString()); });
  • 28.
    var awsIot =require('aws-iot-device-sdk'); var device = awsIot.device({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, region: <YourAWSRegion> }); device.on('connect', function() { console.log('connect'); device.subscribe('topic_1'); device.publish('topic_2', JSON.stringify({ test_data: 1})); }); device.on('message', function(topic, payload) { console.log('message', topic, payload.toString()); });
  • 29.
    var awsIot =require('aws-iot-device-sdk'); var thingShadows = awsIot.thingShadow({...}); … thingShadows.on('connect', function() { thingShadows.register( 'RGBLedLamp', function() { var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}}; clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState ); }); thingShadows.on('status', function(thingName, stat, clientToken, stateObject) {}); thingShadows.on('delta', function(thingName, stateObject) {}); thingShadows.on('timeout', function(thingName, clientToken) {});
  • 30.
    var awsIot =require('aws-iot-device-sdk'); var thingShadows = awsIot.thingShadow({...}); … thingShadows.on('connect', function() { thingShadows.register( 'RGBLedLamp', function() { var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}}; clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState ); }); thingShadows.on('status', function(thingName, stat, clientToken, stateObject) {}); thingShadows.on('delta', function(thingName, stateObject) {}); thingShadows.on('timeout', function(thingName, clientToken) {});
  • 31.
    var awsIot =require('aws-iot-device-sdk'); var thingShadows = awsIot.thingShadow({...}); … thingShadows.on('connect', function() { thingShadows.register( 'RGBLedLamp', function() { var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}}; clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState ); }); thingShadows.on('status', function(thingName, stat, clientToken, stateObject) {}); thingShadows.on('delta', function(thingName, stateObject) {}); thingShadows.on('timeout', function(thingName, clientToken) {});
  • 32.
  • 33.
  • 34.
    Global Network ofRegions and Edge Locations
  • 36.