Quick look up for AWS(Continued..)

2018-04-04

AWS serverless

1
2
3
4
5
6
7
sls config credentials --provider aws --key yourKey --secret yourSecret_Key
sls deploy --stage dev
serverless deploy funciton -f botRequest
serverless deploy

Import node modules to Lambda

1
aws lambda update-function-code --function-name SendSMSFunc --zip-file fileb:///Users/Hanslen/Desktop/yelp-fusion/fusion/node/sendSMS.zip

Connect Lambda Node to API gateway

  1. Make sure the returning object is in the correct format

    Usually in this format!

    1
    2
    3
    4
    5
    6
    var response = {
    'body': "correct",
    'statusCode': 200,
    "headers": { "Access-Control-Allow-Origin":'*'}
    };
    callback(null, response);
  2. How to access the arguments(GET)

    1
    let msg = event["queryStringParameters"]["msg"];

    You can console.log the event to figure out for the POST method!

Connect Lambda Python to API gateway

  1. Make sure returning in the correct format

    1
    2
    3
    4
    5
    6
    7
    8
    return dict(
    statusCode=200,
    headers={
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*'
    },
    body=json.dumps({'message': botmsg})
    )
  2. How to access the arguments(POST)

    1
    2
    receiveData = json.loads(event["body"])
    botmsg = autoReply(receiveData["message"])

AWS S3

1
aws s3 cp myvideo.mp4 s3://mybucket/

Upload file to S3

S3 CORS configuration:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

For React user, we can install

1
npm install --save aws-sdk

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var albumBucketName = 'BUCKET_NAME';
var bucketRegion = 'REGION';
var IdentityPoolId = 'IDENTITYPOOLID';
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: albumBucketName}
});
let files = document.getElementById('uploadImg').files;
if (!files.length) {
return alert('Please choose a file to upload first.');
}
let file = files[0];
let fileName = file.name;
// let albumPhotosKey = encodeURIComponent(albumName) + '//';
let albumPhotosKey = "";
let photoKey = albumPhotosKey + fileName;
let params= {Bucket: albumBucketName, Key: photoKey, Body: file};
s3.upload(params, function(err, data){
console.log(err,data);
});

DynamoDB

Upload Data to the table

1
aws dynamodb batch-write-item --request-items file://~/Downloads/sampledata/ProductCatalog.json

Specific json format can be checked in the file:

https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/samples/sampledata.zip

Connect to DynamoDB with NodeJS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-east-1",
endpoint: "http://dynamodb.us-east-1.amazonaws.com",
accessKeyId:"ACCESSKEYID",
secretAccessKey:"SECRETACCESSKEY"
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});

Search for Item numbers in the table

1
aws dynamodb scan --table-name TABLE_NAME --select "COUNT"

ElasticSearch with Lambda function IAM role

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{ "Version": "2012-10-17", "Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": "*",
"Action": "es:*",
"Resource": "arn:aws:es:us-east-1:ACCOUNTID:domain/myDomain/*"
},
{
"Sid": "",
"Effect": "Allow",
"Principal": "*",
"Action": "es:*",
"Resource": "arn:aws:es:us-east-1:ACCOUNTID:domain/myDomain"
}
]
}

Comments: