title: Upload bulk csv data into existing DynamoDB table
body: <p>I'm trying to migrate data from a <code>csv</code> file into an existing <code>AWS DynamoDB</code> table, as part of an <code>AWS Amplify</code> web app.</p>
  <p>I followed <a href="https://aws.amazon.com/blogs/database/implementing-bulk-csv-ingestion-to-amazon-dynamodb/" rel="nofollow noreferrer">this CloudFormation tutorial</a>, using the below template.</p>
  <p>I was only able to create a new DynamoDB table, but not use an existing table and add data to it.</p>
  <p><strong>QUESTION:</strong>
  Is there a way to modify the template so that I can provide an existing table name at the &quot;Specify stack details&quot; step in the wizard, under &quot;DynamoDBTableName&quot;, so that the csv data will be added to the table? If not, is there an alternative process?</p>
  <pre><code>{
      &quot;AWSTemplateFormatVersion&quot;: &quot;2010-09-09&quot;,
      &quot;Metadata&quot;: {
  
      },
      &quot;Parameters&quot; : {
          &quot;BucketName&quot;: {
              &quot;Description&quot;: &quot;Name of the S3 bucket you will deploy the CSV file to&quot;,
              &quot;Type&quot;: &quot;String&quot;,
              &quot;ConstraintDescription&quot;: &quot;must be a valid bucket name.&quot;
          },
          &quot;FileName&quot;: {
              &quot;Description&quot;: &quot;Name of the S3 file (including suffix)&quot;,
              &quot;Type&quot;: &quot;String&quot;,
              &quot;ConstraintDescription&quot;: &quot;Valid S3 file name.&quot;
          },
          &quot;DynamoDBTableName&quot;: {
              &quot;Description&quot;: &quot;Name of the dynamoDB table you will use&quot;,
              &quot;Type&quot;: &quot;String&quot;,
              &quot;ConstraintDescription&quot;: &quot;must be a valid dynamoDB name.&quot;
          }
      },
      &quot;Resources&quot;: {
          &quot;DynamoDBTable&quot;:{
              &quot;Type&quot;: &quot;AWS::DynamoDB::Table&quot;,
              &quot;Properties&quot;:{
                  &quot;TableName&quot;: {&quot;Ref&quot; : &quot;DynamoDBTableName&quot;},
                  &quot;BillingMode&quot;: &quot;PAY_PER_REQUEST&quot;,
                  &quot;AttributeDefinitions&quot;:[
                      {
                          &quot;AttributeName&quot;: &quot;id&quot;,
                          &quot;AttributeType&quot;: &quot;S&quot;
                      }
                  ],
                  &quot;KeySchema&quot;:[
                      {
                          &quot;AttributeName&quot;: &quot;id&quot;,
                          &quot;KeyType&quot;: &quot;HASH&quot;
                      }
                  ],
                  &quot;Tags&quot;:[
                      {
                          &quot;Key&quot;: &quot;Name&quot;,
                          &quot;Value&quot;: {&quot;Ref&quot; : &quot;DynamoDBTableName&quot;}
                      }
                  ]
              }
          },
          &quot;LambdaRole&quot; : {
            &quot;Type&quot; : &quot;AWS::IAM::Role&quot;,
            &quot;Properties&quot; : {
              &quot;AssumeRolePolicyDocument&quot;: {
                &quot;Version&quot; : &quot;2012-10-17&quot;,
                &quot;Statement&quot; : [
                  {
                    &quot;Effect&quot; : &quot;Allow&quot;,
                    &quot;Principal&quot; : {
                      &quot;Service&quot; : [&quot;lambda.amazonaws.com&quot;,&quot;s3.amazonaws.com&quot;]
                    },
                    &quot;Action&quot; : [
                      &quot;sts:AssumeRole&quot;
                    ]
                  }
                ]
              },
              &quot;Path&quot; : &quot;/&quot;,
              &quot;ManagedPolicyArns&quot;:[&quot;arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole&quot;,&quot;arn:aws:iam::aws:policy/AWSLambdaInvocation-DynamoDB&quot;,&quot;arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess&quot;],
              &quot;Policies&quot;: [{
                               &quot;PolicyName&quot;: &quot;policyname&quot;,
                               &quot;PolicyDocument&quot;: {
                                         &quot;Version&quot;: &quot;2012-10-17&quot;,
                                         &quot;Statement&quot;: [{
                                      &quot;Effect&quot;: &quot;Allow&quot;,
                                                  &quot;Resource&quot;: &quot;*&quot;,
                                                    &quot;Action&quot;: [
                                                                &quot;dynamodb:PutItem&quot;,
                                                                &quot;dynamodb:BatchWriteItem&quot;
                                                    ]
                                        }]
                               }
                      }]
            }
         },
          &quot;CsvToDDBLambdaFunction&quot;: {
              &quot;Type&quot;: &quot;AWS::Lambda::Function&quot;,
              &quot;Properties&quot;: {
                  &quot;Handler&quot;: &quot;index.lambda_handler&quot;,
                  &quot;Role&quot;: {
                      &quot;Fn::GetAtt&quot;: [
                          &quot;LambdaRole&quot;,
                          &quot;Arn&quot;
                      ]
                  },
                  &quot;Code&quot;: {
                      &quot;ZipFile&quot;: {
                          &quot;Fn::Join&quot;: [
                              &quot;\n&quot;,
                              [
                                  &quot;import json&quot;,
                                  &quot;import boto3&quot;,
                                  &quot;import os&quot;,
                                  &quot;import csv&quot;,
                                  &quot;import codecs&quot;,
                                  &quot;import sys&quot;,
                                  &quot;&quot;,
                                  &quot;s3 = boto3.resource('s3')&quot;,
                                  &quot;dynamodb = boto3.resource('dynamodb')&quot;,
                                  &quot;&quot;,
                                  &quot;bucket = os.environ['bucket']&quot;,
                                  &quot;key = os.environ['key']&quot;,
                                  &quot;tableName = os.environ['table']&quot;,
                                  &quot;&quot;,
                                  &quot;def lambda_handler(event, context):&quot;,
                                  &quot;&quot;,  
                                  &quot;&quot;,
                                  &quot;   #get() does not store in memory&quot;,
                                  &quot;   try:&quot;,
                                  &quot;       obj = s3.Object(bucket, key).get()['Body']&quot;,
                                  &quot;   except:&quot;,
                                  &quot;       print(\&quot;S3 Object could not be opened. Check environment variable. \&quot;)&quot;,
                                  &quot;   try:&quot;,
                                  &quot;       table = dynamodb.Table(tableName)&quot;,
                                  &quot;   except:&quot;,
                                  &quot;       print(\&quot;Error loading DynamoDB table. Check if table was created correctly and environment variable.\&quot;)&quot;,
                                  &quot;&quot;,
                                  &quot;   batch_size = 100&quot;,
                                  &quot;   batch = []&quot;,
                                  &quot;&quot;,
                                  &quot;   #DictReader is a generator; not stored in memory&quot;,
                                  &quot;   for row in csv.DictReader(codecs.getreader('utf-8-sig')(obj)):&quot;,
                                  &quot;      if len(batch) &gt;= batch_size:&quot;,
                                  &quot;         write_to_dynamo(batch)&quot;,
                                  &quot;         batch.clear()&quot;,
                                  &quot;&quot;,
                                  &quot;      batch.append(row)&quot;,
                                  &quot;&quot;,
                                  &quot;   if batch:&quot;,
                                  &quot;      write_to_dynamo(batch)&quot;,
                                  &quot;&quot;,
                                  &quot;   return {&quot;,
                                  &quot;      'statusCode': 200,&quot;,
                                  &quot;      'body': json.dumps('Uploaded to DynamoDB Table')&quot;,
                                  &quot;   }&quot;,
                                  &quot;&quot;,
                                  &quot;&quot;,    
                                  &quot;def write_to_dynamo(rows):&quot;,
                                  &quot;   try:&quot;,
                                  &quot;      table = dynamodb.Table(tableName)&quot;,
                                  &quot;   except:&quot;,
                                  &quot;      print(\&quot;Error loading DynamoDB table. Check if table was created correctly and environment variable.\&quot;)&quot;,
                                  &quot;&quot;,
                                  &quot;   try:&quot;,
                                  &quot;      with table.batch_writer() as batch:&quot;,
                                  &quot;         for i in range(len(rows)):&quot;,
                                  &quot;            batch.put_item(&quot;,
                                  &quot;               Item=rows[i]&quot;,
                                  &quot;            )&quot;,
                                  &quot;   except:&quot;,
                                  &quot;      print(\&quot;Error executing batch_writer\&quot;)&quot;
                              ]
                          ]
                      }
                  },
                  &quot;Runtime&quot;: &quot;python3.7&quot;,
                  &quot;Timeout&quot;: 900,
                  &quot;MemorySize&quot;: 3008,
                  &quot;Environment&quot; : {
                      &quot;Variables&quot; : {&quot;bucket&quot; : { &quot;Ref&quot; : &quot;BucketName&quot; }, &quot;key&quot; : { &quot;Ref&quot; : &quot;FileName&quot; },&quot;table&quot; : { &quot;Ref&quot; : &quot;DynamoDBTableName&quot; }}
                  }
              }
          },
  
          &quot;S3Bucket&quot;: {
              &quot;DependsOn&quot; : [&quot;CsvToDDBLambdaFunction&quot;,&quot;BucketPermission&quot;],
              &quot;Type&quot;: &quot;AWS::S3::Bucket&quot;,
              &quot;Properties&quot;: {
  
                  &quot;BucketName&quot;: {&quot;Ref&quot; : &quot;BucketName&quot;},
                  &quot;AccessControl&quot;: &quot;BucketOwnerFullControl&quot;,
                  &quot;NotificationConfiguration&quot;:{
                      &quot;LambdaConfigurations&quot;:[
                          {
                              &quot;Event&quot;:&quot;s3:ObjectCreated:*&quot;,
                              &quot;Function&quot;:{
                                  &quot;Fn::GetAtt&quot;: [
                                      &quot;CsvToDDBLambdaFunction&quot;,
                                      &quot;Arn&quot;
                                  ]
                              }
                          }
                      ]
                  }
              }
          },
          &quot;BucketPermission&quot;:{
              &quot;Type&quot;: &quot;AWS::Lambda::Permission&quot;,
              &quot;Properties&quot;:{
                  &quot;Action&quot;: &quot;lambda:InvokeFunction&quot;,
                  &quot;FunctionName&quot;:{&quot;Ref&quot; : &quot;CsvToDDBLambdaFunction&quot;},
                  &quot;Principal&quot;: &quot;s3.amazonaws.com&quot;,
                  &quot;SourceAccount&quot;: {&quot;Ref&quot;:&quot;AWS::AccountId&quot;}
              }
          }
      },
      &quot;Outputs&quot; : {
  
      }
  }
  
  </code></pre>
  <p><strong>Another answer</strong>
  Dennis' answer is one solution, but you can also comment out the <code>&quot;DynamoDBTable&quot;</code> part within the <code>&quot;Resources&quot;</code> of the JSON file.</p>