Skip to main content
added 4 characters in body
Source Link
Greg Forel
  • 2.6k
  • 6
  • 28
  • 50
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {

    },
    "Parameters" : {
        "BucketName": {
            "Description": "Name of the S3 bucket you will deploy the CSV file to",
            "Type": "String",
            "ConstraintDescription": "must be a valid bucket name."
        },
        "FileName": {
            "Description": "Name of the S3 file (including suffix)",
            "Type": "String",
            "ConstraintDescription": "Valid S3 file name."
        },
        "DynamoDBTableName": {
            "Description": "Name of the dynamoDB table you will use",
            "Type": "String",
            "ConstraintDescription": "must be a valid dynamoDB name."
        }
    },
    "Resources": {
        "DynamoDBTable":{
            "Type": "AWS::DynamoDB::Table",
            "Properties":{
                "TableName": {"Ref" : "DynamoDBTableName"},
                "BillingMode": "PAY_PER_REQUEST",
                "AttributeDefinitions":[
                    {
                        "AttributeName": "id",
                        "AttributeType": "S"
                    }
                ],
                "KeySchema":[
                    {
                        "AttributeName": "id",
                        "KeyType": "HASH"
                    }
                ],
                "Tags":[
                    {
                        "Key": "Name",
                        "Value": {"Ref" : "DynamoDBTableName"}
                    }
                ]
            }
        },
        "LambdaRole" : {
          "Type" : "AWS::IAM::Role",
          "Properties" : {
            "AssumeRolePolicyDocument": {
              "Version" : "2012-10-17",
              "Statement" : [
                {
                  "Effect" : "Allow",
                  "Principal" : {
                    "Service" : ["lambda.amazonaws.com","s3.amazonaws.com"]
                  },
                  "Action" : [
                    "sts:AssumeRole"
                  ]
                }
              ]
            },
            "Path" : "/",
            "ManagedPolicyArns":["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole","arn:aws:iam::aws:policy/AWSLambdaInvocation-DynamoDB","arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"],
            "Policies": [{
                             "PolicyName": "policyname",
                             "PolicyDocument": {
                                       "Version": "2012-10-17",
                                       "Statement": [{
                                    "Effect": "Allow",
                                                "Resource": "*",
                                                  "Action": [
                                                              "dynamodb:PutItem",
                                                              "dynamodb:BatchWriteItem"
                                                  ]
                                      }]
                             }
                    }]
          }
       },
        "CsvToDDBLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "index.lambda_handler",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaRole",
                        "Arn"
                    ]
                },
                "Code": {
                    "ZipFile": {
                        "Fn::Join": [
                            "\n",
                            [
                                "import json",
                                "import boto3",
                                "import os",
                                "import csv",
                                "import codecs",
                                "import sys",
                                "",
                                "s3 = boto3.resource('s3')",
                                "dynamodb = boto3.resource('dynamodb')",
                                "",
                                "bucket = os.environ['bucket']",
                                "key = os.environ['key']",
                                "tableName = os.environ['table']",
                                "",
                                "def lambda_handler(event, context):",
                                "",  
                                "",
                                "   #get() does not store in memory",
                                "   try:",
                                "       obj = s3.Object(bucket, key).get()['Body']",
                                "   except:",
                                "       print(\"S3 Object could not be opened. Check environment variable. \")",
                                "   try:",
                                "       table = dynamodb.Table(tableName)",
                                "   except:",
                                "       print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   batch_size = 100",
                                "   batch = []",
                                "",
                                "   #DictReader is a generator; not stored in memory",
                                "   for row in csv.DictReader(codecs.getreader('utf-8'8-sig')(obj)):",
                                "      if len(batch) >= batch_size:",
                                "         write_to_dynamo(batch)",
                                "         batch.clear()",
                                "",
                                "      batch.append(row)",
                                "",
                                "   if batch:",
                                "      write_to_dynamo(batch)",
                                "",
                                "   return {",
                                "      'statusCode': 200,",
                                "      'body': json.dumps('Uploaded to DynamoDB Table')",
                                "   }",
                                "",
                                "",    
                                "def write_to_dynamo(rows):",
                                "   try:",
                                "      table = dynamodb.Table(tableName)",
                                "   except:",
                                "      print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   try:",
                                "      with table.batch_writer() as batch:",
                                "         for i in range(len(rows)):",
                                "            batch.put_item(",
                                "               Item=rows[i]",
                                "            )",
                                "   except:",
                                "      print(\"Error executing batch_writer\")"
                            ]
                        ]
                    }
                },
                "Runtime": "python3.7",
                "Timeout": 900,
                "MemorySize": 3008,
                "Environment" : {
                    "Variables" : {"bucket" : { "Ref" : "BucketName" }, "key" : { "Ref" : "FileName" },"table" : { "Ref" : "DynamoDBTableName" }}
                }
            }
        },

        "S3Bucket": {
            "DependsOn" : ["CsvToDDBLambdaFunction","BucketPermission"],
            "Type": "AWS::S3::Bucket",
            "Properties": {

                "BucketName": {"Ref" : "BucketName"},
                "AccessControl": "BucketOwnerFullControl",
                "NotificationConfiguration":{
                    "LambdaConfigurations":[
                        {
                            "Event":"s3:ObjectCreated:*",
                            "Function":{
                                "Fn::GetAtt": [
                                    "CsvToDDBLambdaFunction",
                                    "Arn"
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "BucketPermission":{
            "Type": "AWS::Lambda::Permission",
            "Properties":{
                "Action": "lambda:InvokeFunction",
                "FunctionName":{"Ref" : "CsvToDDBLambdaFunction"},
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {"Ref":"AWS::AccountId"}
            }
        }
    },
    "Outputs" : {

    }
}

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {

    },
    "Parameters" : {
        "BucketName": {
            "Description": "Name of the S3 bucket you will deploy the CSV file to",
            "Type": "String",
            "ConstraintDescription": "must be a valid bucket name."
        },
        "FileName": {
            "Description": "Name of the S3 file (including suffix)",
            "Type": "String",
            "ConstraintDescription": "Valid S3 file name."
        },
        "DynamoDBTableName": {
            "Description": "Name of the dynamoDB table you will use",
            "Type": "String",
            "ConstraintDescription": "must be a valid dynamoDB name."
        }
    },
    "Resources": {
        "DynamoDBTable":{
            "Type": "AWS::DynamoDB::Table",
            "Properties":{
                "TableName": {"Ref" : "DynamoDBTableName"},
                "BillingMode": "PAY_PER_REQUEST",
                "AttributeDefinitions":[
                    {
                        "AttributeName": "id",
                        "AttributeType": "S"
                    }
                ],
                "KeySchema":[
                    {
                        "AttributeName": "id",
                        "KeyType": "HASH"
                    }
                ],
                "Tags":[
                    {
                        "Key": "Name",
                        "Value": {"Ref" : "DynamoDBTableName"}
                    }
                ]
            }
        },
        "LambdaRole" : {
          "Type" : "AWS::IAM::Role",
          "Properties" : {
            "AssumeRolePolicyDocument": {
              "Version" : "2012-10-17",
              "Statement" : [
                {
                  "Effect" : "Allow",
                  "Principal" : {
                    "Service" : ["lambda.amazonaws.com","s3.amazonaws.com"]
                  },
                  "Action" : [
                    "sts:AssumeRole"
                  ]
                }
              ]
            },
            "Path" : "/",
            "ManagedPolicyArns":["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole","arn:aws:iam::aws:policy/AWSLambdaInvocation-DynamoDB","arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"],
            "Policies": [{
                             "PolicyName": "policyname",
                             "PolicyDocument": {
                                       "Version": "2012-10-17",
                                       "Statement": [{
                                    "Effect": "Allow",
                                                "Resource": "*",
                                                  "Action": [
                                                              "dynamodb:PutItem",
                                                              "dynamodb:BatchWriteItem"
                                                  ]
                                      }]
                             }
                    }]
          }
       },
        "CsvToDDBLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "index.lambda_handler",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaRole",
                        "Arn"
                    ]
                },
                "Code": {
                    "ZipFile": {
                        "Fn::Join": [
                            "\n",
                            [
                                "import json",
                                "import boto3",
                                "import os",
                                "import csv",
                                "import codecs",
                                "import sys",
                                "",
                                "s3 = boto3.resource('s3')",
                                "dynamodb = boto3.resource('dynamodb')",
                                "",
                                "bucket = os.environ['bucket']",
                                "key = os.environ['key']",
                                "tableName = os.environ['table']",
                                "",
                                "def lambda_handler(event, context):",
                                "",  
                                "",
                                "   #get() does not store in memory",
                                "   try:",
                                "       obj = s3.Object(bucket, key).get()['Body']",
                                "   except:",
                                "       print(\"S3 Object could not be opened. Check environment variable. \")",
                                "   try:",
                                "       table = dynamodb.Table(tableName)",
                                "   except:",
                                "       print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   batch_size = 100",
                                "   batch = []",
                                "",
                                "   #DictReader is a generator; not stored in memory",
                                "   for row in csv.DictReader(codecs.getreader('utf-8')(obj)):",
                                "      if len(batch) >= batch_size:",
                                "         write_to_dynamo(batch)",
                                "         batch.clear()",
                                "",
                                "      batch.append(row)",
                                "",
                                "   if batch:",
                                "      write_to_dynamo(batch)",
                                "",
                                "   return {",
                                "      'statusCode': 200,",
                                "      'body': json.dumps('Uploaded to DynamoDB Table')",
                                "   }",
                                "",
                                "",    
                                "def write_to_dynamo(rows):",
                                "   try:",
                                "      table = dynamodb.Table(tableName)",
                                "   except:",
                                "      print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   try:",
                                "      with table.batch_writer() as batch:",
                                "         for i in range(len(rows)):",
                                "            batch.put_item(",
                                "               Item=rows[i]",
                                "            )",
                                "   except:",
                                "      print(\"Error executing batch_writer\")"
                            ]
                        ]
                    }
                },
                "Runtime": "python3.7",
                "Timeout": 900,
                "MemorySize": 3008,
                "Environment" : {
                    "Variables" : {"bucket" : { "Ref" : "BucketName" }, "key" : { "Ref" : "FileName" },"table" : { "Ref" : "DynamoDBTableName" }}
                }
            }
        },

        "S3Bucket": {
            "DependsOn" : ["CsvToDDBLambdaFunction","BucketPermission"],
            "Type": "AWS::S3::Bucket",
            "Properties": {

                "BucketName": {"Ref" : "BucketName"},
                "AccessControl": "BucketOwnerFullControl",
                "NotificationConfiguration":{
                    "LambdaConfigurations":[
                        {
                            "Event":"s3:ObjectCreated:*",
                            "Function":{
                                "Fn::GetAtt": [
                                    "CsvToDDBLambdaFunction",
                                    "Arn"
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "BucketPermission":{
            "Type": "AWS::Lambda::Permission",
            "Properties":{
                "Action": "lambda:InvokeFunction",
                "FunctionName":{"Ref" : "CsvToDDBLambdaFunction"},
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {"Ref":"AWS::AccountId"}
            }
        }
    },
    "Outputs" : {

    }
}

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {

    },
    "Parameters" : {
        "BucketName": {
            "Description": "Name of the S3 bucket you will deploy the CSV file to",
            "Type": "String",
            "ConstraintDescription": "must be a valid bucket name."
        },
        "FileName": {
            "Description": "Name of the S3 file (including suffix)",
            "Type": "String",
            "ConstraintDescription": "Valid S3 file name."
        },
        "DynamoDBTableName": {
            "Description": "Name of the dynamoDB table you will use",
            "Type": "String",
            "ConstraintDescription": "must be a valid dynamoDB name."
        }
    },
    "Resources": {
        "DynamoDBTable":{
            "Type": "AWS::DynamoDB::Table",
            "Properties":{
                "TableName": {"Ref" : "DynamoDBTableName"},
                "BillingMode": "PAY_PER_REQUEST",
                "AttributeDefinitions":[
                    {
                        "AttributeName": "id",
                        "AttributeType": "S"
                    }
                ],
                "KeySchema":[
                    {
                        "AttributeName": "id",
                        "KeyType": "HASH"
                    }
                ],
                "Tags":[
                    {
                        "Key": "Name",
                        "Value": {"Ref" : "DynamoDBTableName"}
                    }
                ]
            }
        },
        "LambdaRole" : {
          "Type" : "AWS::IAM::Role",
          "Properties" : {
            "AssumeRolePolicyDocument": {
              "Version" : "2012-10-17",
              "Statement" : [
                {
                  "Effect" : "Allow",
                  "Principal" : {
                    "Service" : ["lambda.amazonaws.com","s3.amazonaws.com"]
                  },
                  "Action" : [
                    "sts:AssumeRole"
                  ]
                }
              ]
            },
            "Path" : "/",
            "ManagedPolicyArns":["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole","arn:aws:iam::aws:policy/AWSLambdaInvocation-DynamoDB","arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"],
            "Policies": [{
                             "PolicyName": "policyname",
                             "PolicyDocument": {
                                       "Version": "2012-10-17",
                                       "Statement": [{
                                    "Effect": "Allow",
                                                "Resource": "*",
                                                  "Action": [
                                                              "dynamodb:PutItem",
                                                              "dynamodb:BatchWriteItem"
                                                  ]
                                      }]
                             }
                    }]
          }
       },
        "CsvToDDBLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "index.lambda_handler",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaRole",
                        "Arn"
                    ]
                },
                "Code": {
                    "ZipFile": {
                        "Fn::Join": [
                            "\n",
                            [
                                "import json",
                                "import boto3",
                                "import os",
                                "import csv",
                                "import codecs",
                                "import sys",
                                "",
                                "s3 = boto3.resource('s3')",
                                "dynamodb = boto3.resource('dynamodb')",
                                "",
                                "bucket = os.environ['bucket']",
                                "key = os.environ['key']",
                                "tableName = os.environ['table']",
                                "",
                                "def lambda_handler(event, context):",
                                "",  
                                "",
                                "   #get() does not store in memory",
                                "   try:",
                                "       obj = s3.Object(bucket, key).get()['Body']",
                                "   except:",
                                "       print(\"S3 Object could not be opened. Check environment variable. \")",
                                "   try:",
                                "       table = dynamodb.Table(tableName)",
                                "   except:",
                                "       print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   batch_size = 100",
                                "   batch = []",
                                "",
                                "   #DictReader is a generator; not stored in memory",
                                "   for row in csv.DictReader(codecs.getreader('utf-8-sig')(obj)):",
                                "      if len(batch) >= batch_size:",
                                "         write_to_dynamo(batch)",
                                "         batch.clear()",
                                "",
                                "      batch.append(row)",
                                "",
                                "   if batch:",
                                "      write_to_dynamo(batch)",
                                "",
                                "   return {",
                                "      'statusCode': 200,",
                                "      'body': json.dumps('Uploaded to DynamoDB Table')",
                                "   }",
                                "",
                                "",    
                                "def write_to_dynamo(rows):",
                                "   try:",
                                "      table = dynamodb.Table(tableName)",
                                "   except:",
                                "      print(\"Error loading DynamoDB table. Check if table was created correctly and environment variable.\")",
                                "",
                                "   try:",
                                "      with table.batch_writer() as batch:",
                                "         for i in range(len(rows)):",
                                "            batch.put_item(",
                                "               Item=rows[i]",
                                "            )",
                                "   except:",
                                "      print(\"Error executing batch_writer\")"
                            ]
                        ]
                    }
                },
                "Runtime": "python3.7",
                "Timeout": 900,
                "MemorySize": 3008,
                "Environment" : {
                    "Variables" : {"bucket" : { "Ref" : "BucketName" }, "key" : { "Ref" : "FileName" },"table" : { "Ref" : "DynamoDBTableName" }}
                }
            }
        },

        "S3Bucket": {
            "DependsOn" : ["CsvToDDBLambdaFunction","BucketPermission"],
            "Type": "AWS::S3::Bucket",
            "Properties": {

                "BucketName": {"Ref" : "BucketName"},
                "AccessControl": "BucketOwnerFullControl",
                "NotificationConfiguration":{
                    "LambdaConfigurations":[
                        {
                            "Event":"s3:ObjectCreated:*",
                            "Function":{
                                "Fn::GetAtt": [
                                    "CsvToDDBLambdaFunction",
                                    "Arn"
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "BucketPermission":{
            "Type": "AWS::Lambda::Permission",
            "Properties":{
                "Action": "lambda:InvokeFunction",
                "FunctionName":{"Ref" : "CsvToDDBLambdaFunction"},
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {"Ref":"AWS::AccountId"}
            }
        }
    },
    "Outputs" : {

    }
}

added 154 characters in body
Source Link
Greg Forel
  • 2.6k
  • 6
  • 28
  • 50

Another answer Dennis' answer is one solution, but you can also comment out the "DynamoDBTable" part within the "Resources" of the JSON file.

Another answer Dennis' answer is one solution, but you can also comment out the "DynamoDBTable" part within the "Resources" of the JSON file.

added 19 characters in body
Source Link
Greg Forel
  • 2.6k
  • 6
  • 28
  • 50

QUESTION: Is there a way to modify the template so that I can provide an existing table name at the "Specify stack details" step in the wizard, under "DynamoDBTableName", so that the csv data will be added to the table? If not, is there an alternative process?

QUESTION: Is there a way to modify the template so that I can provide an existing table name at the "Specify stack details", under "DynamoDBTableName", so that the csv data will be added to the table? If not, is there an alternative process?

QUESTION: Is there a way to modify the template so that I can provide an existing table name at the "Specify stack details" step in the wizard, under "DynamoDBTableName", so that the csv data will be added to the table? If not, is there an alternative process?

Source Link
Greg Forel
  • 2.6k
  • 6
  • 28
  • 50
Loading