From a597819e8a964936e6bb9b45c6bcf084333502ad Mon Sep 17 00:00:00 2001 From: Dezo2018 Date: Wed, 21 Sep 2022 17:49:31 -0400 Subject: [PATCH 1/3] Lab 10-kms --- 10-kms/Practice-10.1/PlaintextFile | 1 + 10-kms/Practice-10.1/cmk_key.yml | 55 ++++++++++++++++++ 10-kms/Practice-10.1/encryptedFile | Bin 0 -> 174 bytes 10-kms/Practice-10.1/file.txt | 1 + 10-kms/Practice-10.1/scripts | 14 +++++ 10-kms/Practice-10.2/NewFile.txt | 1 + 10-kms/Practice-10.2/go.mod | 10 ++++ 10-kms/Practice-10.2/go.sum | 22 +++++++ .../Practice-10.2/s3_client_side_download.go | 55 ++++++++++++++++++ 10-kms/Practice-10.2/s3_client_side_upload.go | 55 ++++++++++++++++++ 10 files changed, 214 insertions(+) create mode 100644 10-kms/Practice-10.1/PlaintextFile create mode 100644 10-kms/Practice-10.1/cmk_key.yml create mode 100644 10-kms/Practice-10.1/encryptedFile create mode 100644 10-kms/Practice-10.1/file.txt create mode 100644 10-kms/Practice-10.1/scripts create mode 100644 10-kms/Practice-10.2/NewFile.txt create mode 100644 10-kms/Practice-10.2/go.mod create mode 100644 10-kms/Practice-10.2/go.sum create mode 100644 10-kms/Practice-10.2/s3_client_side_download.go create mode 100644 10-kms/Practice-10.2/s3_client_side_upload.go diff --git a/10-kms/Practice-10.1/PlaintextFile b/10-kms/Practice-10.1/PlaintextFile new file mode 100644 index 00000000..6675f302 --- /dev/null +++ b/10-kms/Practice-10.1/PlaintextFile @@ -0,0 +1 @@ +This is my secret file \ No newline at end of file diff --git a/10-kms/Practice-10.1/cmk_key.yml b/10-kms/Practice-10.1/cmk_key.yml new file mode 100644 index 00000000..704bbc4c --- /dev/null +++ b/10-kms/Practice-10.1/cmk_key.yml @@ -0,0 +1,55 @@ +Description: AWS CMK Key + +Resources: + myKey: + Type: 'AWS::KMS::Key' + Properties: + Description: A symmetric encryption KMS key + EnableKeyRotation: true + PendingWindowInDays: 20 + KeyPolicy: + Version: 2012-10-17 + Id: key-default-1 + Statement: + - Sid: Enable IAM User Permissions + Effect: Allow + Principal: + AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root" + Action: 'kms:*' + Resource: '*' + - Sid: Allow administration of the key + Effect: Allow + Principal: + AWS: !Sub "arn:aws:iam::${AWS::AccountId}:user/desmond.ndambi.labs" + Action: + - 'kms:Create*' + - 'kms:Describe*' + - 'kms:Enable*' + - 'kms:List*' + - 'kms:Put*' + - 'kms:Update*' + - 'kms:Revoke*' + - 'kms:Disable*' + - 'kms:Get*' + - 'kms:Delete*' + - 'kms:ScheduleKeyDeletion' + - 'kms:CancelKeyDeletion' + Resource: '*' + - Sid: Allow use of the key + Effect: Allow + Principal: + AWS: !Sub "arn:aws:iam::${AWS::AccountId}:user/desmond.ndambi.labs" + Action: + - 'kms:DescribeKey' + - 'kms:Encrypt' + - 'kms:Decrypt' + - 'kms:ReEncrypt*' + - 'kms:GenerateDataKey' + - 'kms:GenerateDataKeyWithoutPlaintext' + Resource: '*' + + myAlias: + Type: 'AWS::KMS::Alias' + Properties: + AliasName: alias/ndambi + TargetKeyId: !Ref myKey diff --git a/10-kms/Practice-10.1/encryptedFile b/10-kms/Practice-10.1/encryptedFile new file mode 100644 index 0000000000000000000000000000000000000000..4630c9e4002f05385c117521b5cb0c22ac647e70 GIT binary patch literal 174 zcmZQ%Vq&PcB71b>S3|Z-2d=ss8{P2TaO9D})we2U??Niyzg{@oP@M6}vr4XZz6IN+ zc0PCHcv!xJfq|jKpoooAtIebBJ1-+U+k#YsWF|%igE)j3qk$Y7XF{6?V=6NXqn?2v z3(vIeQ-t3f`FAcqDM*EhQJ}$a+WH;W@`Qd~bjbW_d0g64DDZbj({&fdkp93v<18Pa Xd4GOX{}wiOjPWZ`6W+CSxjrudY>Pw4 literal 0 HcmV?d00001 diff --git a/10-kms/Practice-10.1/file.txt b/10-kms/Practice-10.1/file.txt new file mode 100644 index 00000000..6675f302 --- /dev/null +++ b/10-kms/Practice-10.1/file.txt @@ -0,0 +1 @@ +This is my secret file \ No newline at end of file diff --git a/10-kms/Practice-10.1/scripts b/10-kms/Practice-10.1/scripts new file mode 100644 index 00000000..f624bfba --- /dev/null +++ b/10-kms/Practice-10.1/scripts @@ -0,0 +1,14 @@ +aws kms encrypt \ + --key-id fbc58ad0-2bac-40fe-96ee-5ebd24d2f006 \ + --plaintext fileb://file.txt \ + --output text \ + --query CiphertextBlob | base64 \ + --decode > encryptedFile + +aws kms decrypt \ + --ciphertext-blob fileb://encryptedFile \ + --key-id fbc58ad0-2bac-40fe-96ee-5ebd24d2f006 \ + --output text \ + --query Plaintext | base64 \ + --decode > PlaintextFile + diff --git a/10-kms/Practice-10.2/NewFile.txt b/10-kms/Practice-10.2/NewFile.txt new file mode 100644 index 00000000..158a0601 --- /dev/null +++ b/10-kms/Practice-10.2/NewFile.txt @@ -0,0 +1 @@ +Test Client-Side encryption diff --git a/10-kms/Practice-10.2/go.mod b/10-kms/Practice-10.2/go.mod new file mode 100644 index 00000000..d6845094 --- /dev/null +++ b/10-kms/Practice-10.2/go.mod @@ -0,0 +1,10 @@ +module kms + +go 1.19 + +require ( + github.com/aws/aws-sdk-go v1.44.103 // indirect + github.com/aws/aws-sdk-go-v2 v1.16.16 // indirect + github.com/aws/smithy-go v1.13.3 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect +) diff --git a/10-kms/Practice-10.2/go.sum b/10-kms/Practice-10.2/go.sum new file mode 100644 index 00000000..70c1397e --- /dev/null +++ b/10-kms/Practice-10.2/go.sum @@ -0,0 +1,22 @@ +github.com/aws/aws-sdk-go v1.44.103 h1:tbhBHKgiZSIUkG8FcHy3wYKpPVvp65Wn7ZiX0B8phpY= +github.com/aws/aws-sdk-go v1.44.103/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= +github.com/aws/aws-sdk-go-v2 v1.16.16 h1:M1fj4FE2lB4NzRb9Y0xdWsn2P0+2UHVxwKyOa4YJNjk= +github.com/aws/aws-sdk-go-v2 v1.16.16/go.mod h1:SwiyXi/1zTUZ6KIAmLK5V5ll8SiURNUYOqTerZPaF9k= +github.com/aws/smithy-go v1.13.3 h1:l7LYxGuzK6/K+NzJ2mC+VvLUbae0sL3bXU//04MkmnA= +github.com/aws/smithy-go v1.13.3/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/10-kms/Practice-10.2/s3_client_side_download.go b/10-kms/Practice-10.2/s3_client_side_download.go new file mode 100644 index 00000000..096e6e33 --- /dev/null +++ b/10-kms/Practice-10.2/s3_client_side_download.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3" + "github.com/aws/aws-sdk-go/service/s3/s3crypto" + "os" +) + +var ( + bucket = "kms-bucket-ndambi" + key = "clientside.txt" +) + +func main() { + sess := session.New(&aws.Config{ + Region: aws.String("us-east-1"),}) + + client := s3crypto.NewDecryptionClient(sess) + + input := &s3.GetObjectInput{ + Bucket: &bucket, + Key: &key, + } + + result, err := client.GetObject(input) + // Aside from the S3 errors, here is a list of decryption client errors: + // * InvalidWrapAlgorithmError - returned on an unsupported Wrap algorithm + // * InvalidCEKAlgorithmError - returned on an unsupported CEK algorithm + // * V1NotSupportedError - the SDK doesn’t support v1 because security is an issue for AES ECB + // These errors don’t necessarily mean there’s something wrong. They just tell us we couldn't decrypt some data. + // Users can choose to log this and then continue decrypting the data that they can, or simply return the error. + if err != nil { + log.Fatal(err) + } + + // Let's read the whole body from the response + b, err := ioutil.ReadAll(result.Body) + if err != nil { + log.Fatal(err) + } + //fmt.Println(string(b)) + + file, err := os.Create("NewFile.txt") + if err != nil { + fmt.Println(err) + return + } + fmt.Fprintf(file, "%v\n", string(b)) +} diff --git a/10-kms/Practice-10.2/s3_client_side_upload.go b/10-kms/Practice-10.2/s3_client_side_upload.go new file mode 100644 index 00000000..7f885b6b --- /dev/null +++ b/10-kms/Practice-10.2/s3_client_side_upload.go @@ -0,0 +1,55 @@ +/* +Licensed under the MIT-0 license https://github.com/aws/mit-0 +*/ +package main + +import ( + "log" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/kms" + "github.com/aws/aws-sdk-go/service/s3" + "github.com/aws/aws-sdk-go/service/s3/s3crypto" +) + +var ( + cmkId = "fbc58ad0-2bac-40fe-96ee-5ebd24d2f006" + bucket = "kms-bucket-ndambi" + key = "clientside.txt" +) + +func main() { + sess, err := session.NewSession(&aws.Config{ + Region: aws.String("us-east-1"), + Credentials: credentials.NewSharedCredentials("", "default"), + }) + // This is our key wrap handler, used to generate cipher keys and IVs for + // our cipher builder. Using an IV allows more “spontaneous” encryption. + // The IV makes it more difficult for hackers to use dictionary attacks. + // The key wrap handler behaves as the master key. Without it, you can’t + // encrypt or decrypt the data. + keywrap := s3crypto.NewKMSKeyGenerator(kms.New(sess), cmkId) + // This is our content cipher builder, used to instantiate new ciphers + // that enable us to encrypt or decrypt the payload. + builder := s3crypto.AESGCMContentCipherBuilder(keywrap) + // Let's create our crypto client! + client := s3crypto.NewEncryptionClient(sess, builder) + + input := &s3.PutObjectInput{ + Bucket: &bucket, + Key: &key, + Body: strings.NewReader("Test Client-Side encryption"), + } + + _, err = client.PutObject(input) + // What to expect as errors? You can expect any sort of S3 errors, http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html. + // The s3crypto client can also return some errors: + // * MissingCMKIDError - when using AWS KMS, the user must specify their key's ARN + if err != nil { + log.Fatal(err) + } +} + From c9056754893bbf19830f5f0c788e9f8d1e9af36f Mon Sep 17 00:00:00 2001 From: Dezo2018 Date: Thu, 22 Sep 2022 10:57:05 -0400 Subject: [PATCH 2/3] removing files --- 10-kms/Practice-10.1/PlaintextFile | 1 - 10-kms/Practice-10.1/cmk_key.yml | 55 ------------------ 10-kms/Practice-10.1/encryptedFile | Bin 174 -> 0 bytes 10-kms/Practice-10.1/file.txt | 1 - 10-kms/Practice-10.1/scripts | 14 ----- 10-kms/Practice-10.2/NewFile.txt | 1 - 10-kms/Practice-10.2/go.mod | 10 ---- 10-kms/Practice-10.2/go.sum | 22 ------- .../Practice-10.2/s3_client_side_download.go | 55 ------------------ 10-kms/Practice-10.2/s3_client_side_upload.go | 55 ------------------ 10 files changed, 214 deletions(-) delete mode 100644 10-kms/Practice-10.1/PlaintextFile delete mode 100644 10-kms/Practice-10.1/cmk_key.yml delete mode 100644 10-kms/Practice-10.1/encryptedFile delete mode 100644 10-kms/Practice-10.1/file.txt delete mode 100644 10-kms/Practice-10.1/scripts delete mode 100644 10-kms/Practice-10.2/NewFile.txt delete mode 100644 10-kms/Practice-10.2/go.mod delete mode 100644 10-kms/Practice-10.2/go.sum delete mode 100644 10-kms/Practice-10.2/s3_client_side_download.go delete mode 100644 10-kms/Practice-10.2/s3_client_side_upload.go diff --git a/10-kms/Practice-10.1/PlaintextFile b/10-kms/Practice-10.1/PlaintextFile deleted file mode 100644 index 6675f302..00000000 --- a/10-kms/Practice-10.1/PlaintextFile +++ /dev/null @@ -1 +0,0 @@ -This is my secret file \ No newline at end of file diff --git a/10-kms/Practice-10.1/cmk_key.yml b/10-kms/Practice-10.1/cmk_key.yml deleted file mode 100644 index 704bbc4c..00000000 --- a/10-kms/Practice-10.1/cmk_key.yml +++ /dev/null @@ -1,55 +0,0 @@ -Description: AWS CMK Key - -Resources: - myKey: - Type: 'AWS::KMS::Key' - Properties: - Description: A symmetric encryption KMS key - EnableKeyRotation: true - PendingWindowInDays: 20 - KeyPolicy: - Version: 2012-10-17 - Id: key-default-1 - Statement: - - Sid: Enable IAM User Permissions - Effect: Allow - Principal: - AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root" - Action: 'kms:*' - Resource: '*' - - Sid: Allow administration of the key - Effect: Allow - Principal: - AWS: !Sub "arn:aws:iam::${AWS::AccountId}:user/desmond.ndambi.labs" - Action: - - 'kms:Create*' - - 'kms:Describe*' - - 'kms:Enable*' - - 'kms:List*' - - 'kms:Put*' - - 'kms:Update*' - - 'kms:Revoke*' - - 'kms:Disable*' - - 'kms:Get*' - - 'kms:Delete*' - - 'kms:ScheduleKeyDeletion' - - 'kms:CancelKeyDeletion' - Resource: '*' - - Sid: Allow use of the key - Effect: Allow - Principal: - AWS: !Sub "arn:aws:iam::${AWS::AccountId}:user/desmond.ndambi.labs" - Action: - - 'kms:DescribeKey' - - 'kms:Encrypt' - - 'kms:Decrypt' - - 'kms:ReEncrypt*' - - 'kms:GenerateDataKey' - - 'kms:GenerateDataKeyWithoutPlaintext' - Resource: '*' - - myAlias: - Type: 'AWS::KMS::Alias' - Properties: - AliasName: alias/ndambi - TargetKeyId: !Ref myKey diff --git a/10-kms/Practice-10.1/encryptedFile b/10-kms/Practice-10.1/encryptedFile deleted file mode 100644 index 4630c9e4002f05385c117521b5cb0c22ac647e70..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 174 zcmZQ%Vq&PcB71b>S3|Z-2d=ss8{P2TaO9D})we2U??Niyzg{@oP@M6}vr4XZz6IN+ zc0PCHcv!xJfq|jKpoooAtIebBJ1-+U+k#YsWF|%igE)j3qk$Y7XF{6?V=6NXqn?2v z3(vIeQ-t3f`FAcqDM*EhQJ}$a+WH;W@`Qd~bjbW_d0g64DDZbj({&fdkp93v<18Pa Xd4GOX{}wiOjPWZ`6W+CSxjrudY>Pw4 diff --git a/10-kms/Practice-10.1/file.txt b/10-kms/Practice-10.1/file.txt deleted file mode 100644 index 6675f302..00000000 --- a/10-kms/Practice-10.1/file.txt +++ /dev/null @@ -1 +0,0 @@ -This is my secret file \ No newline at end of file diff --git a/10-kms/Practice-10.1/scripts b/10-kms/Practice-10.1/scripts deleted file mode 100644 index f624bfba..00000000 --- a/10-kms/Practice-10.1/scripts +++ /dev/null @@ -1,14 +0,0 @@ -aws kms encrypt \ - --key-id fbc58ad0-2bac-40fe-96ee-5ebd24d2f006 \ - --plaintext fileb://file.txt \ - --output text \ - --query CiphertextBlob | base64 \ - --decode > encryptedFile - -aws kms decrypt \ - --ciphertext-blob fileb://encryptedFile \ - --key-id fbc58ad0-2bac-40fe-96ee-5ebd24d2f006 \ - --output text \ - --query Plaintext | base64 \ - --decode > PlaintextFile - diff --git a/10-kms/Practice-10.2/NewFile.txt b/10-kms/Practice-10.2/NewFile.txt deleted file mode 100644 index 158a0601..00000000 --- a/10-kms/Practice-10.2/NewFile.txt +++ /dev/null @@ -1 +0,0 @@ -Test Client-Side encryption diff --git a/10-kms/Practice-10.2/go.mod b/10-kms/Practice-10.2/go.mod deleted file mode 100644 index d6845094..00000000 --- a/10-kms/Practice-10.2/go.mod +++ /dev/null @@ -1,10 +0,0 @@ -module kms - -go 1.19 - -require ( - github.com/aws/aws-sdk-go v1.44.103 // indirect - github.com/aws/aws-sdk-go-v2 v1.16.16 // indirect - github.com/aws/smithy-go v1.13.3 // indirect - github.com/jmespath/go-jmespath v0.4.0 // indirect -) diff --git a/10-kms/Practice-10.2/go.sum b/10-kms/Practice-10.2/go.sum deleted file mode 100644 index 70c1397e..00000000 --- a/10-kms/Practice-10.2/go.sum +++ /dev/null @@ -1,22 +0,0 @@ -github.com/aws/aws-sdk-go v1.44.103 h1:tbhBHKgiZSIUkG8FcHy3wYKpPVvp65Wn7ZiX0B8phpY= -github.com/aws/aws-sdk-go v1.44.103/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= -github.com/aws/aws-sdk-go-v2 v1.16.16 h1:M1fj4FE2lB4NzRb9Y0xdWsn2P0+2UHVxwKyOa4YJNjk= -github.com/aws/aws-sdk-go-v2 v1.16.16/go.mod h1:SwiyXi/1zTUZ6KIAmLK5V5ll8SiURNUYOqTerZPaF9k= -github.com/aws/smithy-go v1.13.3 h1:l7LYxGuzK6/K+NzJ2mC+VvLUbae0sL3bXU//04MkmnA= -github.com/aws/smithy-go v1.13.3/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/10-kms/Practice-10.2/s3_client_side_download.go b/10-kms/Practice-10.2/s3_client_side_download.go deleted file mode 100644 index 096e6e33..00000000 --- a/10-kms/Practice-10.2/s3_client_side_download.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "log" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/s3" - "github.com/aws/aws-sdk-go/service/s3/s3crypto" - "os" -) - -var ( - bucket = "kms-bucket-ndambi" - key = "clientside.txt" -) - -func main() { - sess := session.New(&aws.Config{ - Region: aws.String("us-east-1"),}) - - client := s3crypto.NewDecryptionClient(sess) - - input := &s3.GetObjectInput{ - Bucket: &bucket, - Key: &key, - } - - result, err := client.GetObject(input) - // Aside from the S3 errors, here is a list of decryption client errors: - // * InvalidWrapAlgorithmError - returned on an unsupported Wrap algorithm - // * InvalidCEKAlgorithmError - returned on an unsupported CEK algorithm - // * V1NotSupportedError - the SDK doesn’t support v1 because security is an issue for AES ECB - // These errors don’t necessarily mean there’s something wrong. They just tell us we couldn't decrypt some data. - // Users can choose to log this and then continue decrypting the data that they can, or simply return the error. - if err != nil { - log.Fatal(err) - } - - // Let's read the whole body from the response - b, err := ioutil.ReadAll(result.Body) - if err != nil { - log.Fatal(err) - } - //fmt.Println(string(b)) - - file, err := os.Create("NewFile.txt") - if err != nil { - fmt.Println(err) - return - } - fmt.Fprintf(file, "%v\n", string(b)) -} diff --git a/10-kms/Practice-10.2/s3_client_side_upload.go b/10-kms/Practice-10.2/s3_client_side_upload.go deleted file mode 100644 index 7f885b6b..00000000 --- a/10-kms/Practice-10.2/s3_client_side_upload.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Licensed under the MIT-0 license https://github.com/aws/mit-0 -*/ -package main - -import ( - "log" - "strings" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/kms" - "github.com/aws/aws-sdk-go/service/s3" - "github.com/aws/aws-sdk-go/service/s3/s3crypto" -) - -var ( - cmkId = "fbc58ad0-2bac-40fe-96ee-5ebd24d2f006" - bucket = "kms-bucket-ndambi" - key = "clientside.txt" -) - -func main() { - sess, err := session.NewSession(&aws.Config{ - Region: aws.String("us-east-1"), - Credentials: credentials.NewSharedCredentials("", "default"), - }) - // This is our key wrap handler, used to generate cipher keys and IVs for - // our cipher builder. Using an IV allows more “spontaneous” encryption. - // The IV makes it more difficult for hackers to use dictionary attacks. - // The key wrap handler behaves as the master key. Without it, you can’t - // encrypt or decrypt the data. - keywrap := s3crypto.NewKMSKeyGenerator(kms.New(sess), cmkId) - // This is our content cipher builder, used to instantiate new ciphers - // that enable us to encrypt or decrypt the payload. - builder := s3crypto.AESGCMContentCipherBuilder(keywrap) - // Let's create our crypto client! - client := s3crypto.NewEncryptionClient(sess, builder) - - input := &s3.PutObjectInput{ - Bucket: &bucket, - Key: &key, - Body: strings.NewReader("Test Client-Side encryption"), - } - - _, err = client.PutObject(input) - // What to expect as errors? You can expect any sort of S3 errors, http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html. - // The s3crypto client can also return some errors: - // * MissingCMKIDError - when using AWS KMS, the user must specify their key's ARN - if err != nil { - log.Fatal(err) - } -} - From e1dfbb0423e6986a2145c314d65e33770a2585f6 Mon Sep 17 00:00:00 2001 From: Dezo2018 Date: Fri, 21 Oct 2022 12:02:57 -0400 Subject: [PATCH 3/3] Lab-16 SAM --- 16-SAM/Practice-16.1/sam-app/.gitignore | 345 ++++++++++++++++++ 16-SAM/Practice-16.1/sam-app/README.md | 126 +++++++ 16-SAM/Practice-16.1/sam-app/__init__.py | 0 16-SAM/Practice-16.1/sam-app/env.json | 5 + .../Practice-16.1/sam-app/events/custom.json | 123 +++++++ .../Practice-16.1/sam-app/events/event.json | 62 ++++ .../sam-app/hello_world/__init__.py | 0 .../Practice-16.1/sam-app/hello_world/app.py | 43 +++ .../sam-app/hello_world/requirements.txt | 1 + 16-SAM/Practice-16.1/sam-app/packaged.yaml | 42 +++ 16-SAM/Practice-16.1/sam-app/template.yaml | 45 +++ .../Practice-16.1/sam-app/tests/__init__.py | 0 .../sam-app/tests/unit/__init__.py | 0 .../sam-app/tests/unit/test_handler.py | 72 ++++ 16-SAM/Practice-16.2/sam-api/.gitignore | 345 ++++++++++++++++++ 16-SAM/Practice-16.2/sam-api/README.md | 126 +++++++ 16-SAM/Practice-16.2/sam-api/__init__.py | 0 .../Practice-16.2/sam-api/events/event.json | 62 ++++ .../sam-api/hello_world/__init__.py | 0 .../Practice-16.2/sam-api/hello_world/app.py | 39 ++ .../sam-api/hello_world/requirements.txt | 1 + 16-SAM/Practice-16.2/sam-api/template.yaml | 39 ++ .../Practice-16.2/sam-api/tests/__init__.py | 0 .../sam-api/tests/unit/__init__.py | 0 .../sam-api/tests/unit/test_handler.py | 72 ++++ 16-SAM/Practice-16.2/sam-s3/__init__.py | 0 16-SAM/Practice-16.2/sam-s3/events/event.json | 131 +++++++ 16-SAM/Practice-16.2/sam-s3/events/sns.json | 31 ++ 16-SAM/Practice-16.2/sam-s3/packaged.yaml | 108 ++++++ .../sam-s3/process-s3-sub/__init__.py | 0 .../sam-s3/process-s3-sub/app.py | 39 ++ .../sam-s3/process-s3/__init__.py | 0 16-SAM/Practice-16.2/sam-s3/process-s3/app.py | 42 +++ 16-SAM/Practice-16.2/sam-s3/template.yml | 91 +++++ 16-SAM/Practice-16.2/sam-sqs/__init__.py | 0 16-SAM/Practice-16.2/sam-sqs/config.sh | 2 + .../Practice-16.2/sam-sqs/events/event.json | 20 + 16-SAM/Practice-16.2/sam-sqs/packaged.yaml | 44 +++ .../sam-sqs/process_sqs/__init__.py | 0 .../Practice-16.2/sam-sqs/process_sqs/app.py | 8 + .../sam-sqs/process_sqs/requirements.txt | 1 + 16-SAM/Practice-16.2/sam-sqs/response.json | 1 + 16-SAM/Practice-16.2/sam-sqs/template.yaml | 36 ++ 16-SAM/Practice-16.3/sam-dynamoDb/.gitignore | 244 +++++++++++++ 16-SAM/Practice-16.3/sam-dynamoDb/README.md | 130 +++++++ 16-SAM/Practice-16.3/sam-dynamoDb/__init__.py | 0 .../sam-dynamoDb/create-table.json | 13 + .../sam-dynamoDb/docker-compose.yml | 17 + .../docker/dynamodb/shared-local-instance.db | Bin 0 -> 16384 bytes .../sam-dynamoDb/events/event.json | 93 +++++ .../sam-dynamoDb/hello_world/__init__.py | 0 .../sam-dynamoDb/hello_world/app.py | 56 +++ .../sam-dynamoDb/hello_world/requirements.txt | 1 + .../Practice-16.3/sam-dynamoDb/packaged.yaml | 59 +++ .../sam-dynamoDb/streamdb/__init__.py | 0 .../sam-dynamoDb/streamdb/app.py | 4 + .../Practice-16.3/sam-dynamoDb/template.yaml | 80 ++++ .../sam-dynamoDb/tests/__init__.py | 0 .../tests/integration/__init__.py | 0 .../tests/integration/test_api_gateway.py | 45 +++ .../sam-dynamoDb/tests/requirements.txt | 3 + .../sam-dynamoDb/tests/unit/__init__.py | 0 .../sam-dynamoDb/tests/unit/test_handler.py | 72 ++++ 16-SAM/Practice-16.4/sam-app/.gitignore | 129 +++++++ 16-SAM/Practice-16.4/sam-app/README.md | 126 +++++++ .../sam-app/dynamodb/shared-local-instance.db | Bin 0 -> 16384 bytes .../Practice-16.4/sam-app/events/event.json | 62 ++++ .../sam-app/hello-world/.npmignore | 1 + .../Practice-16.4/sam-app/hello-world/app.js | 33 ++ .../sam-app/hello-world/package.json | 19 + .../hello-world/tests/unit/test-handler.js | 22 ++ 16-SAM/Practice-16.4/sam-app/lifecycleHook.js | 35 ++ 16-SAM/Practice-16.4/sam-app/packaged.yaml | 114 ++++++ 16-SAM/Practice-16.4/sam-app/template.yaml | 101 +++++ 16-SAM/commands | 14 + 16-SAM/create-table.json | 2 +- 76 files changed, 3576 insertions(+), 1 deletion(-) create mode 100644 16-SAM/Practice-16.1/sam-app/.gitignore create mode 100644 16-SAM/Practice-16.1/sam-app/README.md create mode 100644 16-SAM/Practice-16.1/sam-app/__init__.py create mode 100644 16-SAM/Practice-16.1/sam-app/env.json create mode 100644 16-SAM/Practice-16.1/sam-app/events/custom.json create mode 100644 16-SAM/Practice-16.1/sam-app/events/event.json create mode 100644 16-SAM/Practice-16.1/sam-app/hello_world/__init__.py create mode 100644 16-SAM/Practice-16.1/sam-app/hello_world/app.py create mode 100644 16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt create mode 100644 16-SAM/Practice-16.1/sam-app/packaged.yaml create mode 100644 16-SAM/Practice-16.1/sam-app/template.yaml create mode 100644 16-SAM/Practice-16.1/sam-app/tests/__init__.py create mode 100644 16-SAM/Practice-16.1/sam-app/tests/unit/__init__.py create mode 100644 16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py create mode 100644 16-SAM/Practice-16.2/sam-api/.gitignore create mode 100644 16-SAM/Practice-16.2/sam-api/README.md create mode 100644 16-SAM/Practice-16.2/sam-api/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-api/events/event.json create mode 100644 16-SAM/Practice-16.2/sam-api/hello_world/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-api/hello_world/app.py create mode 100644 16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt create mode 100644 16-SAM/Practice-16.2/sam-api/template.yaml create mode 100644 16-SAM/Practice-16.2/sam-api/tests/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-api/tests/unit/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py create mode 100644 16-SAM/Practice-16.2/sam-s3/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-s3/events/event.json create mode 100644 16-SAM/Practice-16.2/sam-s3/events/sns.json create mode 100644 16-SAM/Practice-16.2/sam-s3/packaged.yaml create mode 100644 16-SAM/Practice-16.2/sam-s3/process-s3-sub/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py create mode 100644 16-SAM/Practice-16.2/sam-s3/process-s3/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-s3/process-s3/app.py create mode 100644 16-SAM/Practice-16.2/sam-s3/template.yml create mode 100644 16-SAM/Practice-16.2/sam-sqs/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-sqs/config.sh create mode 100644 16-SAM/Practice-16.2/sam-sqs/events/event.json create mode 100644 16-SAM/Practice-16.2/sam-sqs/packaged.yaml create mode 100644 16-SAM/Practice-16.2/sam-sqs/process_sqs/__init__.py create mode 100644 16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py create mode 100644 16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt create mode 100644 16-SAM/Practice-16.2/sam-sqs/response.json create mode 100644 16-SAM/Practice-16.2/sam-sqs/template.yaml create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/.gitignore create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/README.md create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/create-table.json create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/events/event.json create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/hello_world/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/streamdb/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/template.yaml create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/__init__.py create mode 100644 16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py create mode 100644 16-SAM/Practice-16.4/sam-app/.gitignore create mode 100644 16-SAM/Practice-16.4/sam-app/README.md create mode 100644 16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db create mode 100644 16-SAM/Practice-16.4/sam-app/events/event.json create mode 100644 16-SAM/Practice-16.4/sam-app/hello-world/.npmignore create mode 100644 16-SAM/Practice-16.4/sam-app/hello-world/app.js create mode 100644 16-SAM/Practice-16.4/sam-app/hello-world/package.json create mode 100644 16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js create mode 100644 16-SAM/Practice-16.4/sam-app/lifecycleHook.js create mode 100644 16-SAM/Practice-16.4/sam-app/packaged.yaml create mode 100644 16-SAM/Practice-16.4/sam-app/template.yaml create mode 100644 16-SAM/commands diff --git a/16-SAM/Practice-16.1/sam-app/.gitignore b/16-SAM/Practice-16.1/sam-app/.gitignore new file mode 100644 index 00000000..4bccb52c --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/.gitignore @@ -0,0 +1,345 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam +# Edit at https://www.toptal.com/developers/gitignore?templates=osx,linux,python,windows,pycharm,visualstudiocode,sam + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +# .env +.env/ +.venv/ +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# operating system-related files +# file properties cache/storage on macOS +*.DS_Store +# thumbnail cache on Windows +Thumbs.db + +# profiling data +.prof + + +### SAM ### +# Ignore build directories for the AWS Serverless Application Model (SAM) +# Info: https://aws.amazon.com/serverless/sam/ +# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html + +**/.aws-sam + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam diff --git a/16-SAM/Practice-16.1/sam-app/README.md b/16-SAM/Practice-16.1/sam-app/README.md new file mode 100644 index 00000000..c895f9c8 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/README.md @@ -0,0 +1,126 @@ +# sam-app + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-app$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-app$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-app$ sam local start-api +sam-app$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-app$ sam logs -n HelloWorldFunction --stack-name sam-app --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the [pytest](https://docs.pytest.org/en/latest/) and run unit tests. + +```bash +sam-app$ pip install pytest pytest-mock --user +sam-app$ python -m pytest tests/ -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-app +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.1/sam-app/__init__.py b/16-SAM/Practice-16.1/sam-app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/env.json b/16-SAM/Practice-16.1/sam-app/env.json new file mode 100644 index 00000000..2cc92943 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/env.json @@ -0,0 +1,5 @@ +{ + "HelloWorldFunction": { + "NAME": "desmond.ndambi.labs" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/events/custom.json b/16-SAM/Practice-16.1/sam-app/events/custom.json new file mode 100644 index 00000000..e379b8cd --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/events/custom.json @@ -0,0 +1,123 @@ +{ + "body": "eyJ0ZXN0IjoiYm9keSJ9", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": true, + "queryStringParameters": { + "foo": "bar" + }, + "multiValueQueryStringParameters": { + "foo": [ + "bar" + ] + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "multiValueHeaders": { + "Accept": [ + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" + ], + "Accept-Encoding": [ + "gzip, deflate, sdch" + ], + "Accept-Language": [ + "en-US,en;q=0.8" + ], + "Cache-Control": [ + "max-age=0" + ], + "CloudFront-Forwarded-Proto": [ + "https" + ], + "CloudFront-Is-Desktop-Viewer": [ + "true" + ], + "CloudFront-Is-Mobile-Viewer": [ + "false" + ], + "CloudFront-Is-SmartTV-Viewer": [ + "false" + ], + "CloudFront-Is-Tablet-Viewer": [ + "false" + ], + "CloudFront-Viewer-Country": [ + "US" + ], + "Host": [ + "0123456789.execute-api.us-east-1.amazonaws.com" + ], + "Upgrade-Insecure-Requests": [ + "1" + ], + "User-Agent": [ + "Custom User Agent String" + ], + "Via": [ + "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)" + ], + "X-Amz-Cf-Id": [ + "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==" + ], + "X-Forwarded-For": [ + "127.0.0.1, 127.0.0.2" + ], + "X-Forwarded-Port": [ + "443" + ], + "X-Forwarded-Proto": [ + "https" + ] + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/events/event.json b/16-SAM/Practice-16.1/sam-app/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/__init__.py b/16-SAM/Practice-16.1/sam-app/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/app.py b/16-SAM/Practice-16.1/sam-app/hello_world/app.py new file mode 100644 index 00000000..e283b1a8 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/hello_world/app.py @@ -0,0 +1,43 @@ +import json +import os + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + return { + "statusCode": 200, + "body": json.dumps({ + "message": "hello world", + "NAME": os.environ.get('NAME') + # "location": ip.text.replace("\n", "") + }), + } diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt b/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.1/sam-app/packaged.yaml b/16-SAM/Practice-16.1/sam-app/packaged.yaml new file mode 100644 index 00000000..c0290bdc --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/packaged.yaml @@ -0,0 +1,42 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-app + + Sample SAM Template for sam-app + + ' +Globals: + Function: + Timeout: 3 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/1007ba6f2fd6aed6daf87b6f392f298d + Handler: app.lambda_handler + Runtime: python3.7 + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get + Metadata: + SamResourceId: HelloWorldFunction +Outputs: + HelloWorldApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/ + HelloWorldFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - HelloWorldFunction + - Arn + HelloWorldFunctionIamRole: + Description: Implicit IAM Role created for Hello World function + Value: + Fn::GetAtt: + - HelloWorldFunctionRole + - Arn diff --git a/16-SAM/Practice-16.1/sam-app/template.yaml b/16-SAM/Practice-16.1/sam-app/template.yaml new file mode 100644 index 00000000..a7e04a4b --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/template.yaml @@ -0,0 +1,45 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Parameters: + Name: + Type: String +Description: > + sam-app + + Sample SAM Template for sam-app + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.7 + Environment: + Variables: + NAME: !Ref Name + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/Practice-16.1/sam-app/tests/__init__.py b/16-SAM/Practice-16.1/sam-app/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/tests/unit/__init__.py b/16-SAM/Practice-16.1/sam-app/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py b/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.2/sam-api/.gitignore b/16-SAM/Practice-16.2/sam-api/.gitignore new file mode 100644 index 00000000..4bccb52c --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/.gitignore @@ -0,0 +1,345 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam +# Edit at https://www.toptal.com/developers/gitignore?templates=osx,linux,python,windows,pycharm,visualstudiocode,sam + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +# .env +.env/ +.venv/ +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# operating system-related files +# file properties cache/storage on macOS +*.DS_Store +# thumbnail cache on Windows +Thumbs.db + +# profiling data +.prof + + +### SAM ### +# Ignore build directories for the AWS Serverless Application Model (SAM) +# Info: https://aws.amazon.com/serverless/sam/ +# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html + +**/.aws-sam + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam diff --git a/16-SAM/Practice-16.2/sam-api/README.md b/16-SAM/Practice-16.2/sam-api/README.md new file mode 100644 index 00000000..18ec17d7 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/README.md @@ -0,0 +1,126 @@ +# sam-api + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-api$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-api$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-api$ sam local start-api +sam-api$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-api$ sam logs -n HelloWorldFunction --stack-name sam-api --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the [pytest](https://docs.pytest.org/en/latest/) and run unit tests. + +```bash +sam-api$ pip install pytest pytest-mock --user +sam-api$ python -m pytest tests/ -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-api +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.2/sam-api/__init__.py b/16-SAM/Practice-16.2/sam-api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/events/event.json b/16-SAM/Practice-16.2/sam-api/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/__init__.py b/16-SAM/Practice-16.2/sam-api/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/app.py b/16-SAM/Practice-16.2/sam-api/hello_world/app.py new file mode 100644 index 00000000..732b1352 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/hello_world/app.py @@ -0,0 +1,39 @@ +import json + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + + return { + "statusCode": 200, + "body": event['pathStringParameters'], + } diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt b/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-api/template.yaml b/16-SAM/Practice-16.2/sam-api/template.yaml new file mode 100644 index 00000000..70c9f0dc --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/template.yaml @@ -0,0 +1,39 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-api + + Sample SAM Template for sam-api + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.7 + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/Practice-16.2/sam-api/tests/__init__.py b/16-SAM/Practice-16.2/sam-api/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/tests/unit/__init__.py b/16-SAM/Practice-16.2/sam-api/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py b/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.2/sam-s3/__init__.py b/16-SAM/Practice-16.2/sam-s3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/events/event.json b/16-SAM/Practice-16.2/sam-s3/events/event.json new file mode 100644 index 00000000..84f760b6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/events/event.json @@ -0,0 +1,131 @@ +{ + "Records": [ + { + "eventVersion": "2.0", + "eventSource": "aws:s3", + "awsRegion": "us-east-1", + "eventTime": "1970-01-01T00:00:00.000Z", + "eventName": "ObjectCreated:Put", + "userIdentity": { + "principalId": "EXAMPLE" + }, + "requestParameters": { + "sourceIPAddress": "127.0.0.1" + }, + "responseElements": { + "x-amz-request-id": "EXAMPLE123456789", + "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH" + }, + "s3": { + "s3SchemaVersion": "1.0", + "configurationId": "testConfigRule", + "bucket": { + "name": "desmond-sam-bucket", + "ownerIdentity": { + "principalId": "EXAMPLE" + }, + "arn": "arn:aws:s3:::desmond-sam-bucket" + }, + "object": { + "key": "test/key", + "size": 1024, + "eTag": "0123456789abcdef0123456789abcdef", + "sequencer": "0A1B2C3D4E5F678901" + } + } + } + ] +} +{ + "Records": [ + { + "eventID": "c4ca4238a0b923820dcc509a6f75849b", + "eventName": "INSERT", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439091", + "SizeBytes": 26, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "c81e728d9d4c2f636f067f89cc14862c", + "eventName": "MODIFY", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439092", + "SizeBytes": 59, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "eccbc87e4b5ce2fe28308fd9f2a7baf3", + "eventName": "REMOVE", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439093", + "SizeBytes": 38, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-s3/events/sns.json b/16-SAM/Practice-16.2/sam-s3/events/sns.json new file mode 100644 index 00000000..5e9bd5cd --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/events/sns.json @@ -0,0 +1,31 @@ +{ + "Records": [ + { + "EventSource": "aws:sns", + "EventVersion": "1.0", + "EventSubscriptionArn": "arn:aws:sns:us-east-1::SampleTopic", + "Sns": { + "Type": "Notification", + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", + "TopicArn": "arn:aws:sns:us-east-1:123456789012:SampleTopic", + "Subject": "example subject", + "Message": "This lab is crazy", + "Timestamp": "1970-01-01T00:00:00.000Z", + "SignatureVersion": "1", + "Signature": "EXAMPLE", + "SigningCertUrl": "EXAMPLE", + "UnsubscribeUrl": "EXAMPLE", + "MessageAttributes": { + "Test": { + "Type": "String", + "Value": "TestString" + }, + "TestBinary": { + "Type": "Binary", + "Value": "TestBinary" + } + } + } + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-s3/packaged.yaml b/16-SAM/Practice-16.2/sam-s3/packaged.yaml new file mode 100644 index 00000000..7d6086e8 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/packaged.yaml @@ -0,0 +1,108 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-s3 + + Sample SAM Template for s3 + + ' +Globals: + Function: + Timeout: 3 +Resources: + s3Bucket: + Type: AWS::S3::Bucket + Properties: + BucketName: desmond-sam-bucket + Metadata: + SamResourceId: s3Bucket + SNSTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: SampleTopic + Metadata: + SamResourceId: SNSTopic + s3Function: + Type: AWS::Serverless::Function + DependsOn: + - SNSTopic + Properties: + CodeUri: s3://sam-bucket-desmond/f4a62871816679d0980b132eadcd3a82 + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - s3:GetObject + Resource: '*' + Events: + S3Event: + Type: S3 + Properties: + Bucket: + Ref: s3Bucket + Events: s3:ObjectCreated:* + SNSEvent: + Type: SNS + Properties: + Topic: + Ref: SNSTopic + Metadata: + SamResourceId: s3Function + Subscription: + Type: AWS::SNS::Subscription + DependsOn: + - SNSTopic + Properties: + Endpoint: desmond.ndambi@stelligent.com + Protocol: email + TopicArn: + Ref: SNSTopic + Metadata: + SamResourceId: Subscription + s3SubscripFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/b0ba279429f18268bc73cadf7319512e + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - sns:CreateTopic + - sns:Publish + - sns:Subscribe + - sns:CreateTopic + - sns:GetTopicAttributes + - sns:SetTopicAttributes + - sns:TagResource + - sns:UntagResource + - sns:ListTagsForResource + - sns:ListSubscriptionsByTopic + - s3:GetObject + - s3:ListBucket + Resource: '*' + Events: + SNSEvent: + Type: SNS + Properties: + Topic: + Ref: SNSTopic + Metadata: + SamResourceId: s3SubscripFunction +Outputs: + s3Function: + Description: Process S3 Function ARN + Value: + Fn::GetAtt: + - s3Function + - Arn + s3SubscripFunction: + Description: Process S3 Subscription Function ARN + Value: + Fn::GetAtt: + - s3SubscripFunction + - Arn diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3-sub/__init__.py b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py new file mode 100644 index 00000000..901869ce --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py @@ -0,0 +1,39 @@ +import boto3 + +print('Loading function') + +sns = boto3.client('sns') +s3 = boto3.resource('s3') + + +def lambda_handler(event, context): + """ + Subscribes an endpoint to the topic. Some endpoint types, such as email, + must be confirmed before their subscriptions are active. When a subscription + is not confirmed, its Amazon Resource Number (ARN) is set to + 'PendingConfirmation'. + + :param topic: The topic to subscribe to. + :param protocol: The protocol of the endpoint, such as 'sms' or 'email'. + :param endpoint: The endpoint that receives messages, such as a phone number + or an email address. + :return: The newly added subscription. + """ + topic = sns.create_topic(Name='SampleTopic') + topic_arn = topic["TopicArn"] + # Create email subscription + response = sns.subscribe(TopicArn=topic_arn, Protocol="email", Endpoint="desmond.ndambi@stelligent.com") + subscription_arn = response["SubscriptionArn"] + + s3_bucket_name='desmond-sam-bucket' + + my_bucket=s3.Bucket(s3_bucket_name) + bucket_list = [] + for file in my_bucket.objects.all(): + # Un comment this to specify a files with particular extensions e.g .json. .csv + # file_name=file.key + # if file_name.find(".json")!=-1: + bucket_list.append(file.key) + length_bucket_list=print(len(bucket_list)) + print(bucket_list[0:10]) + diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3/__init__.py b/16-SAM/Practice-16.2/sam-s3/process-s3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3/app.py b/16-SAM/Practice-16.2/sam-s3/process-s3/app.py new file mode 100644 index 00000000..6def667f --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/process-s3/app.py @@ -0,0 +1,42 @@ +from __future__ import print_function +import json +import urllib.parse +import boto3 + +print('Loading function') + +s3 = boto3.client('s3') +sns = boto3.resource('sns') + + +def lambda_handler(event, context): + #print("Received event: " + json.dumps(event, indent=2)) + + # Get the object from the event and show its content type + bucket = event['Records'][0]['s3']['bucket']['name'] + key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') + try: + response = s3.get_object(Bucket=bucket, Key=key) + print("CONTENT TYPE: " + response['ContentType']) + return response['ContentType'] + except Exception as e: + print(e) + print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) + raise e + + #print("Received event: " + json.dumps(event, indent=2)) + message = event['Records'][0]['Sns']['Message'] + print("From SNS: " + message) + + # Lambda function publish message to an SNS Topic + topic = sns.create_topic(Name='SampleTopic') + topic_arn = topic.arn + response = sns.publish ( + TargetArn = topic_arn, + Message = json.dumps({'default': message}), + MessageStructure = 'json' + ) + return { + 'statusCode': 200, + 'body': json.dumps(response) + } diff --git a/16-SAM/Practice-16.2/sam-s3/template.yml b/16-SAM/Practice-16.2/sam-s3/template.yml new file mode 100644 index 00000000..370e38ab --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/template.yml @@ -0,0 +1,91 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-s3 + + Sample SAM Template for s3 + +Globals: + Function: + Timeout: 3 + +Resources: + s3Bucket: + Type: AWS::S3::Bucket + Properties: + BucketName: "desmond-sam-bucket" + SNSTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: "SampleTopic" + s3Function: + Type: AWS::Serverless::Function + DependsOn: + - SNSTopic + Properties: + CodeUri: process-s3/ + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - s3:GetObject + Resource: '*' + Events: + S3Event: + Type: S3 + Properties: + Bucket: !Ref s3Bucket + Events: s3:ObjectCreated:* + SNSEvent: + Type: SNS + Properties: + Topic: !Ref SNSTopic + Subscription: + Type: AWS::SNS::Subscription + DependsOn: + - SNSTopic + Properties: + Endpoint: desmond.ndambi@stelligent.com + Protocol: email + TopicArn: !Ref 'SNSTopic' + s3SubscripFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: process-s3-sub/ + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - sns:CreateTopic + - sns:Publish + - sns:Subscribe + - sns:CreateTopic + - sns:GetTopicAttributes + - sns:SetTopicAttributes + - sns:TagResource + - sns:UntagResource + - sns:ListTagsForResource + - sns:ListSubscriptionsByTopic + - s3:GetObject + - s3:ListBucket + Resource: "*" + Events: + SNSEvent: + Type: SNS + Properties: + Topic: !Ref SNSTopic + + +Outputs: + s3Function: + Description: "Process S3 Function ARN" + Value: !GetAtt s3Function.Arn + s3SubscripFunction: + Description: "Process S3 Subscription Function ARN" + Value: !GetAtt s3SubscripFunction.Arn diff --git a/16-SAM/Practice-16.2/sam-sqs/__init__.py b/16-SAM/Practice-16.2/sam-sqs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-sqs/config.sh b/16-SAM/Practice-16.2/sam-sqs/config.sh new file mode 100644 index 00000000..735e2e85 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/config.sh @@ -0,0 +1,2 @@ +#To invoke the lambda function with the generate event +aws lambda invoke --function-name SAM-SQS-ProcessSqsFunction-9VpBgMnNIswj --cli-binary-format raw-in-base64-out --payload file://events/event.json response.json --profile labs diff --git a/16-SAM/Practice-16.2/sam-sqs/events/event.json b/16-SAM/Practice-16.2/sam-sqs/events/event.json new file mode 100644 index 00000000..f697abde --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/events/event.json @@ -0,0 +1,20 @@ +{ + "Records": [ + { + "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78", + "receiptHandle": "MessageReceiptHandle", + "body": "Hello from SQS!", + "attributes": { + "ApproximateReceiveCount": "1", + "SentTimestamp": "1523232000000", + "SenderId": "123456789012", + "ApproximateFirstReceiveTimestamp": "1523232000001" + }, + "messageAttributes": {}, + "md5OfBody": "7b270e59b47ff90a553787216d55d91d", + "eventSource": "aws:sqs", + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue", + "awsRegion": "us-east-1" + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-sqs/packaged.yaml b/16-SAM/Practice-16.2/sam-sqs/packaged.yaml new file mode 100644 index 00000000..a9151c16 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/packaged.yaml @@ -0,0 +1,44 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-api + + Sample SAM Template for sam-sqs + + ' +Globals: + Function: + Timeout: 3 +Resources: + MessageQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: SampleQueue + Metadata: + SamResourceId: MessageQueue + ProcessSqsFunction: + Type: AWS::Serverless::Function + DependsOn: + - MessageQueue + Properties: + CodeUri: s3://sam-bucket-desmond/3154cf92b3f3b7dcb3b4cdfe0d1b6958 + Handler: app.lambda_handler + Runtime: python3.7 + Events: + SQSQueue: + Type: SQS + Properties: + Queue: + Fn::GetAtt: + - MessageQueue + - Arn + BatchSize: 10 + Enabled: false + Metadata: + SamResourceId: ProcessSqsFunction +Outputs: + ProcessSqsFunction: + Description: Process Queue Function ARN + Value: + Fn::GetAtt: + - ProcessSqsFunction + - Arn diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/__init__.py b/16-SAM/Practice-16.2/sam-sqs/process_sqs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py b/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py new file mode 100644 index 00000000..4a5da424 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py @@ -0,0 +1,8 @@ +import json + +# import requests + +def lambda_handler(event, context): + for record in event['Records']: + payload=record["body"] + print(str(payload)) diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt b/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-sqs/response.json b/16-SAM/Practice-16.2/sam-sqs/response.json new file mode 100644 index 00000000..ec747fa4 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/response.json @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-sqs/template.yaml b/16-SAM/Practice-16.2/sam-sqs/template.yaml new file mode 100644 index 00000000..14871ed4 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/template.yaml @@ -0,0 +1,36 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-sqs + + Sample SAM Template for sam-sqs + +Globals: + Function: + Timeout: 3 + +Resources: + MessageQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: "SampleQueue" + ProcessSqsFunction: + Type: AWS::Serverless::Function + DependsOn: + - MessageQueue + Properties: + CodeUri: process_sqs/ + Handler: app.lambda_handler + Runtime: python3.7 + Events: + SQSQueue: + Type: SQS + Properties: + Queue: !GetAtt MessageQueue.Arn + BatchSize: 10 + Enabled: false + +Outputs: + ProcessSqsFunction: + Description: "Process Queue Function ARN" + Value: !GetAtt ProcessSqsFunction.Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore b/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore new file mode 100644 index 00000000..4808264d --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore @@ -0,0 +1,244 @@ + +# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Ruby plugin and RubyMine +/.rakeTasks + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +.pytest_cache/ +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +.history + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Build folder + +*/build/* + +# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode \ No newline at end of file diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/README.md b/16-SAM/Practice-16.3/sam-dynamoDb/README.md new file mode 100644 index 00000000..68134eb2 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/README.md @@ -0,0 +1,130 @@ +# sam-dynamoDb + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-dynamoDb$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-dynamoDb$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-dynamoDb$ sam local start-api +sam-dynamoDb$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-dynamoDb$ sam logs -n HelloWorldFunction --stack-name sam-dynamoDb --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests. + +```bash +sam-dynamoDb$ pip install -r tests/requirements.txt --user +# unit test +sam-dynamoDb$ python -m pytest tests/unit -v +# integration test, requiring deploying the stack first. +# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing +sam-dynamoDb$ AWS_SAM_STACK_NAME= python -m pytest tests/integration -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-dynamoDb +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json b/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json new file mode 100644 index 00000000..b25872c6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json @@ -0,0 +1,13 @@ +{ + "TableName": "DynamoDBTable", + "KeySchema": [ + { "AttributeName": "id", "KeyType": "HASH" } + ], + "AttributeDefinitions": [ + { "AttributeName": "id", "AttributeType": "S" } + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + } +} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml b/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml new file mode 100644 index 00000000..417a6875 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.5' + +services: + dynamodb-local: + container_name: dynamodb-local + image: "amazon/dynamodb-local:latest" + ports: + - "8000:8000" + working_dir: /home/dynamodblocal + command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data" + volumes: + - "./docker/dynamodb:/home/dynamodblocal/data" + networks: + - api + +networks: + api: diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db b/16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db new file mode 100644 index 0000000000000000000000000000000000000000..068c7f4a29826d20e12594346f428d36b137f951 GIT binary patch literal 16384 zcmeI2&u`l{6vy==v7KLA0t^Ut<1}E72RCiDU_*176T68P$IdLJYfuycW6_NYOHM_) zn^)xEq}cAm{*C=7`v-Q|ZRmBkU5WvFlw`|vlPn#KqE0Btv8YE%?|sN8GS=7nrtC6; z8m+*?41KOlD5|P_g^;2s8hqyA)7z%uV4_!mZ*}1KqC-u&^)fxlE9rVOWmmc)Adl17Qt2eX);aOj(iCp|dIBU7hp>x0wEUmZ)E=jb1xy zw;bF_TGj}5Ht;d7yR5SZLxLuzZv*J73v8j&W&(EIZ8ly1@lMO>dxeUndxgESjqc>p zF7E7-pvK1zC1XfVSiD+?-feQ#_TTVN`F_8=Ano-at#wNI)T1 z5LBN|&B0$)|674wZjyi`a3ct0RPDoOpDnL0-wE?yTT@_{nu0+ zv1yrQmojJ;6^ew`lM`0BYBl+a%2<1Dgct8Q(7z|Q^b7xS$GovkI{Rb@6h5i>{4e}hf4$?E(MbZ5zyJY0|I7Rz03r`1fg40X&i^;4vt_7~ zzz_jB{|^C?MM>ZW5s>r$4eD$esw6N(faiZo`Bj1a5A73eTl+^h-i|gWapcPSpEnRU z@b@t}DL;9e@{?<-VY>3@97}Z?&;PWVQPhljOZ`*xw4e2T{TFpL$<7fTb&E6U#cWnR z-DF&-CcKCc3hxP`B}1?oXeoXV8;ace;;G7BxtvGbbRRd{Bow@%O4UY@0i;}><^FK4 zI;(p4hC#&j zG9lrhC&EQR#WsDJij4&aD=e6MhX)C%v#+gcWq4=}MjQ{#T<-2{YH=rfk?d0#(FZF+ z@%<-r>BZYwwL23^L`il^fQ3bQ09*Z(7YzR62T z0%Jx%{{A;+9VXqC1V%_e&i^BXDK8}nj2Qtr|BqRRNp~fI5fYI3KSG%DQj)-!5%?Fl C&nFE4 literal 0 HcmV?d00001 diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json b/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json new file mode 100644 index 00000000..4458c684 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json @@ -0,0 +1,93 @@ +{ + "Records": [ + { + "eventID": "c4ca4238a0b923820dcc509a6f75849b", + "eventName": "INSERT", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439091", + "SizeBytes": 26, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "c81e728d9d4c2f636f067f89cc14862c", + "eventName": "MODIFY", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439092", + "SizeBytes": 59, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "eccbc87e4b5ce2fe28308fd9f2a7baf3", + "eventName": "REMOVE", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439093", + "SizeBytes": 38, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + } + ] +} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py new file mode 100644 index 00000000..cf5e916c --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py @@ -0,0 +1,56 @@ +import json +import os +import boto3 +import uuid + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + # Create records + table_name = os.getenv('TABLE_NAME') + if os.getenv('AWS_SAM_LOCAL'): + table = boto3.resource('dynamodb', endpoint_url="http://0.0.0.0:8000/").Table(table_name) + # Inserting an item in a table + table.put_item(Item={'id': str(uuid.uuid4())}) + else: + region = os.getenv('AWS_REGION') + table = boto3.resource('dynamodb', region_name=region).Table(table_name) + + + return { + "statusCode": 200, + "body": json.dumps({ + "message": "hello world", + "TABLE_NAME": os.environ.get('TABLE_NAME'), + # "location": ip.text.replace("\n", "") + }), + } diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml b/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml new file mode 100644 index 00000000..279da6e6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-dynamoDb + + Sample SAM Template for sam-dynamoDb + + ' +Globals: + Function: + Timeout: 3 +Resources: + DynamoDBTable: + Type: AWS::DynamoDB::Table + Properties: + TableName: my-table + AttributeDefinitions: + - AttributeName: id + AttributeType: S + KeySchema: + - AttributeName: id + KeyType: HASH + ProvisionedThroughput: + ReadCapacityUnits: 5 + WriteCapacityUnits: 5 + Metadata: + SamResourceId: DynamoDBTable + WriteFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: s3://sam-bucket-desmond/3716a846e5024c1f2d3b61aae5d6822a + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Events: + HelloWorld: + Type: Api + Properties: + Path: /write + Method: get + Environment: + Variables: + TABLE_NAME: + Ref: DynamoDBTable + Metadata: + SamResourceId: WriteFunction +Outputs: + DynamoApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/write/ + WriteFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - WriteFunction + - Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py new file mode 100644 index 00000000..2e65e21b --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py @@ -0,0 +1,4 @@ +def lambda_handler(event, context): + for record in event['Records']: + print(record['eventID']) + print(record['eventName']) diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml b/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml new file mode 100644 index 00000000..2a5d0411 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml @@ -0,0 +1,80 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-dynamoDb + + Sample SAM Template for sam-dynamoDb + +Globals: + Function: + Timeout: 3 + +Resources: + DynamoDBTable: + Type: AWS::DynamoDB::Table + Properties: + TableName: my-table + AttributeDefinitions: + - AttributeName: id + AttributeType: S + KeySchema: + - AttributeName: id + KeyType: HASH + ProvisionedThroughput: + ReadCapacityUnits: 5 + WriteCapacityUnits: 5 + StreamSpecification: + StreamViewType: NEW_IMAGE + WriteFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Policies: + - DynamoDBCrudPolicy: + TableName: !Ref DynamoDBTable + Events: + HelloWorld: + Type: Api + Properties: + Path: /write + Method: get + Environment: + Variables: + TABLE_NAME: !Ref DynamoDBTable + ProcessStreamEventsFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: streamdb/ + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Policies: + - DynamoDBStreamReadPolicy: + TableName: !Ref DynamoDBTable + StreamName: !GetAtt DynamoDBTable.StreamArn + Events: + DBStreams: + Type: DynamoDB + Properties: + Stream: !GetAtt DynamoDBTable.StreamArn + StartingPosition: TRIM_HORIZON + BatchSize: 10 + MaximumBatchingWindowInSeconds: 10 + Enabled: false + +Outputs: + DynamoApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/write/" + WriteFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt WriteFunction.Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py new file mode 100644 index 00000000..b96e8033 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py @@ -0,0 +1,45 @@ +import os + +import boto3 +import pytest +import requests + +""" +Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test. +""" + + +class TestApiGateway: + + @pytest.fixture() + def api_gateway_url(self): + """ Get the API Gateway URL from Cloudformation Stack outputs """ + stack_name = os.environ.get("AWS_SAM_STACK_NAME") + + if stack_name is None: + raise ValueError('Please set the AWS_SAM_STACK_NAME environment variable to the name of your stack') + + client = boto3.client("cloudformation") + + try: + response = client.describe_stacks(StackName=stack_name) + except Exception as e: + raise Exception( + f"Cannot find stack {stack_name} \n" f'Please make sure a stack with the name "{stack_name}" exists' + ) from e + + stacks = response["Stacks"] + stack_outputs = stacks[0]["Outputs"] + api_outputs = [output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"] + + if not api_outputs: + raise KeyError(f"HelloWorldAPI not found in stack {stack_name}") + + return api_outputs[0]["OutputValue"] # Extract url from stack outputs + + def test_api_gateway(self, api_gateway_url): + """ Call the API Gateway endpoint and check the response """ + response = requests.get(api_gateway_url) + + assert response.status_code == 200 + assert response.json() == {"message": "hello world"} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt b/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt new file mode 100644 index 00000000..b9cf27ab --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt @@ -0,0 +1,3 @@ +pytest +boto3 +requests diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.4/sam-app/.gitignore b/16-SAM/Practice-16.4/sam-app/.gitignore new file mode 100644 index 00000000..eb1db5fb --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/.gitignore @@ -0,0 +1,129 @@ + +# Created by https://www.gitignore.io/api/osx,node,linux,windows + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +# End of https://www.gitignore.io/api/osx,node,linux,windows \ No newline at end of file diff --git a/16-SAM/Practice-16.4/sam-app/README.md b/16-SAM/Practice-16.4/sam-app/README.md new file mode 100644 index 00000000..9bd0bf19 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/README.md @@ -0,0 +1,126 @@ +# sam-app + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello-world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- hello-world/tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Node.js - [Install Node.js 10](https://nodejs.org/en/), including the NPM package management tool. +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build` command. + +```bash +sam-app$ sam build +``` + +The SAM CLI installs dependencies defined in `hello-world/package.json`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-app$ sam local invoke putItemFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-app$ sam local start-api +sam-app$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-app$ sam logs -n HelloWorldFunction --stack-name sam-app --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `hello-world/tests` folder in this project. Use NPM to install the [Mocha test framework](https://mochajs.org/) and run unit tests. + +```bash +sam-app$ cd hello-world +hello-world$ npm install +hello-world$ npm run test +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-app +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db b/16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db new file mode 100644 index 0000000000000000000000000000000000000000..3a99419b686e2e3ab8be87a8fb15488607603c0a GIT binary patch literal 16384 zcmeI2O>g5w7{~3n%}ZaPJw=GiV8o_@q?>kA(v}NplWk42X^GvkWwoj%_GIfSc3OL~ z5UL*1uHeK4A;f_rSH1-o5FcQL#0}8i5Em{hBxYI4flh{e^F8M$oeEs~_sAfvEIg%k zhJ~m6XQ4Un5nmO4>NINK3rFJP@Zj2uNk6xKym9Kmpa!@fOL{u@37p+G`?HMdhYZMV5@A zh5gn+W6{)n(;BppiGyyeRir!l$|hJTZcccXWb*Y$IG!8lIo;II$M#;G;NF5w+;H++ zw!JlF+tK#T0SCdMOVGvzq>|3I5$$TfH>gE2 zkF&V_x?1bBvz3MhTYk$5g4!b7g{qEQ>tsmSzUc#t^wkMA$7#t2yP((Wy7}NvL+g44 z3dP&`^^yWIDX<1xYsjk5u|`7Z8>NAYJwvDqhR_yHqpcCznnCC0z$720Q^_4hYE7+$ z0=(54n0SaRo6HqJst<)n$zNXhiz5e1i~u8WEeJ&U&?^t8C$p0oXa0xgIdZVX2s{IU zqeXrsF&K?TW3f+_XtdBWq0uNTIExm4^xIpJZ}!DJ#+F`fVq}Z+_jNxCC$)4l|8d8ZxPn@q z1Iu}NnfT@2W3f?h8m8BJ8`|5XUe`z#a_BXp`z_-5K}sZ>2(rpMzIkC^2+ul>5wN~* z_Ydf%o)`Y$$iWgLzzAGz0%JlV)_EK`PingU2mY(S+VQjK7y(A0hX7sw+5GPT!cG~1 zYeayp|JSIqS*VOa9|5-h_W@y9M&KF|VC(-i>TDJ&BhW{H&i^p?9Y>B|LN`Jyp}z(3 z`Dk-|N6xJOc?NL#~1 zv-2F!+J)grJdxlJmoOEo1zto1GA{*0OJcw#f(h?EtRr&kil@r!rBVt|(*~?Jkt28m z;T>$z(embKGvbDm-jqYb4+{wulPPf~E~ss+E+q zj-1dhll2EC5gJ-9*VBZAy`BUvN>pq^r>WSmwKavU(F;6Ckc!`vSIhlFt2bhAXeN_$ zBjNa+#7VNxz=+F@h%9?6k1sX#NS}C zH-Vg4l4Se;b9}zBOEChMi~#%n?~-*GbC(e~M*?*H=Y`KXa3<({!s8biIJ-9^zzCctfrOyM&hiLw_5V*e;mLX4on4C& QU<57_fw+*39k*xr2lC=yrvLx| literal 0 HcmV?d00001 diff --git a/16-SAM/Practice-16.4/sam-app/events/event.json b/16-SAM/Practice-16.4/sam-app/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore b/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore new file mode 100644 index 00000000..e7e1fb04 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore @@ -0,0 +1 @@ +tests/* diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/app.js b/16-SAM/Practice-16.4/sam-app/hello-world/app.js new file mode 100644 index 00000000..4701236a --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/app.js @@ -0,0 +1,33 @@ +// const axios = require('axios') +// const url = 'http://checkip.amazonaws.com/'; +let response; + +/** + * + * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + * @param {Object} event - API Gateway Lambda Proxy Input Format + * + * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html + * @param {Object} context + * + * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + * @returns {Object} object - API Gateway Lambda Proxy Output Format + * + */ +exports.lambdaHandler = async (event, context) => { + try { + // const ret = await axios(url); + response = { + 'statusCode': 200, + 'body': JSON.stringify({ + message: 'hello desmond', + // location: ret.data.trim() + }) + } + } catch (err) { + console.log(err); + return err; + } + + return response +}; diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/package.json b/16-SAM/Practice-16.4/sam-app/hello-world/package.json new file mode 100644 index 00000000..78ddd1ac --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/package.json @@ -0,0 +1,19 @@ +{ + "name": "hello_world", + "version": "1.0.0", + "description": "hello world sample for NodeJS", + "main": "app.js", + "repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs", + "author": "SAM CLI", + "license": "MIT", + "dependencies": { + "axios": "^0.21.1" + }, + "scripts": { + "test": "mocha tests/unit/" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^6.1.4" + } +} diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js b/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js new file mode 100644 index 00000000..ae94b9f2 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js @@ -0,0 +1,22 @@ +'use strict'; + +const app = require('../../app.js'); +const chai = require('chai'); +const expect = chai.expect; +var event, context; + +describe('Tests index', function () { + it('verifies successful response', async () => { + const result = await app.lambdaHandler(event, context) + + expect(result).to.be.an('object'); + expect(result.statusCode).to.equal(200); + expect(result.body).to.be.an('string'); + + let response = JSON.parse(result.body); + + expect(response).to.be.an('object'); + expect(response.message).to.be.equal("hello world"); + // expect(response.location).to.be.an("string"); + }); +}); diff --git a/16-SAM/Practice-16.4/sam-app/lifecycleHook.js b/16-SAM/Practice-16.4/sam-app/lifecycleHook.js new file mode 100644 index 00000000..e99acacc --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/lifecycleHook.js @@ -0,0 +1,35 @@ +'use strict'; + +const aws = require('aws-sdk'); +const codedeploy = new aws.CodeDeploy({ apiVersion: '2014-10-06' }); + +exports.handler = (event, context, callback) => { + //Read the DeploymentId from the event payload. + var deploymentId = event.DeploymentId; + + //Read the LifecycleEventHookExecutionId from the event payload + var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId; + + /* + Enter validation tests here. + */ + + // Prepare the validation test results with the deploymentId and + // the lifecycleEventHookExecutionId for AWS CodeDeploy. + var params = { + deploymentId: deploymentId, + lifecycleEventHookExecutionId: lifecycleEventHookExecutionId, + status: 'Succeeded' // status can be 'Succeeded' or 'Failed' + }; + + // Pass AWS CodeDeploy the prepared validation test results. + codedeploy.putLifecycleEventHookExecutionStatus(params, function (err, data) { + if (err) { + // Validation failed. + callback('Validation test failed'); + } else { + // Validation succeeded. + callback(null, 'Validation test succeeded'); + } + }); +}; \ No newline at end of file diff --git a/16-SAM/Practice-16.4/sam-app/packaged.yaml b/16-SAM/Practice-16.4/sam-app/packaged.yaml new file mode 100644 index 00000000..8387dbc5 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/packaged.yaml @@ -0,0 +1,114 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-app + + Sample SAM Template for sam-app + + ' +Globals: + Function: + Timeout: 3 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/7201c45d112e260889aa90899fc0ab5e + Handler: app.lambdaHandler + Runtime: nodejs14.x + Architectures: + - arm64 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery10Minutes + Hooks: + PreTraffic: + Ref: beforeAllowTraffic + PostTraffic: + Ref: afterAllowTraffic + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get + Metadata: + SamResourceId: HelloWorldFunction + beforeAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - codedeploy:PutLifecycleEventHookExecutionStatus + Resource: + Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - lambda:InvokeFunction + Resource: + Ref: HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: CodeDeployHook_beforeAllowTraffic + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: + Ref: HelloWorldFunction.Version + CodeUri: s3://sam-bucket-desmond/c973d46b9711c2fecf426d9ae063c5f5 + Metadata: + SamResourceId: beforeAllowTraffic + afterAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - codedeploy:PutLifecycleEventHookExecutionStatus + Resource: + Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - lambda:InvokeFunction + Resource: + Ref: HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: CodeDeployHook_afterAllowTraffic + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: + Ref: HelloWorldFunction.Version + CodeUri: s3://sam-bucket-desmond/c973d46b9711c2fecf426d9ae063c5f5 + Metadata: + SamResourceId: afterAllowTraffic +Outputs: + HelloWorldApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/ + HelloWorldFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - HelloWorldFunction + - Arn + HelloWorldFunctionIamRole: + Description: Implicit IAM Role created for Hello World function + Value: + Fn::GetAtt: + - HelloWorldFunctionRole + - Arn diff --git a/16-SAM/Practice-16.4/sam-app/template.yaml b/16-SAM/Practice-16.4/sam-app/template.yaml new file mode 100644 index 00000000..500d0c05 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/template.yaml @@ -0,0 +1,101 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-app + + Sample SAM Template for sam-app + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello-world/ + Handler: app.lambdaHandler + Runtime: nodejs14.x + Architectures: + - arm64 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery10Minutes + Hooks: + PreTraffic: !Ref beforeAllowTraffic + PostTraffic: !Ref afterAllowTraffic + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + + beforeAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "codedeploy:PutLifecycleEventHookExecutionStatus" + Resource: + !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "lambda:InvokeFunction" + Resource: !Ref HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: 'CodeDeployHook_beforeAllowTraffic' + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: !Ref HelloWorldFunction.Version + + afterAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "codedeploy:PutLifecycleEventHookExecutionStatus" + Resource: + !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "lambda:InvokeFunction" + Resource: !Ref HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: 'CodeDeployHook_afterAllowTraffic' + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: !Ref HelloWorldFunction.Version + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/commands b/16-SAM/commands new file mode 100644 index 00000000..4ea4db20 --- /dev/null +++ b/16-SAM/commands @@ -0,0 +1,14 @@ +sam build && sam package --output-template-file packaged.yaml --s3-bucket sam-bucket-desmond --profile labs +sam deploy --template-file packaged.yaml --capabilities CAPABILITY_IAM \ +--stack-name SAM-DynamoDB --profile labs + + +aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/324320755747/SampleQueue \ + --message-body events/event.json \ + --delay-seconds 10 \ + --profile labs + +aws dynamodb create-table --cli-input-json file://create-table.json \ +--endpoint-url http://localhost:8000 + +aws dynamodb scan --table-name DynamoDBTable --endpoint-url http://localhost:8000 diff --git a/16-SAM/create-table.json b/16-SAM/create-table.json index b9772498..b25872c6 100644 --- a/16-SAM/create-table.json +++ b/16-SAM/create-table.json @@ -1,5 +1,5 @@ { - "TableName": "", + "TableName": "DynamoDBTable", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ],