# CloudFormation cannot update a stack when a custom-named resource requires replacing

**URL:** <https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [November 30, 2017, 6:52am UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217 "2017-11-30T06:52:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![x11joe](https://avatars.discourse-cdn.com/v4/letter/x/50afbb/32.png) [@x11joe](https://forum.serverless.com/u/x11joe)\
**Post date:** [November 30, 2017, 6:52am UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/1 "2017-11-30T06:52:22Z")

</div>

I have the following error.

Serverless: Operation failed!

Serverless Error ---------------------------------------

An error occurred: phoneNumberTable - CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename mysite-api-phonenumber-dev and update the stack again…

I tried deleting the database to see if it could re-create it then but it still gives the same error and doesn’t remake the database? What do I do here?

I put more info here in my stackoverflow question as well. [https://stackoverflow.com/questions/47567549/serverless-error-cloudformation-cannot-update-a-stack-whena-custom-named-resour](https://stackoverflow.com/questions/47567549/serverless-error-cloudformation-cannot-update-a-stack-whena-custom-named-resour)

---

<div class="post-metadata">

**Author:** ![jonsmirl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonsmirl/32/776_2.png) [@jonsmirl](https://forum.serverless.com/u/jonsmirl)\
**Post date:** [November 30, 2017, 1:25pm UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/2 "2017-11-30T13:25:49Z")

</div>

I hate this error too. I think it is a security feature of AWS. You are forced to change the name of phoneNumberTable to something like phoneNumberTable1. Then the next time you get the error change it back to phoneNumberTable. Deleting the resource will not work.

Maybe someone can enlighten us on a way around it.

The AWS recommended solution is to rename:  
[https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-custom-name/](https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-custom-name/)

---

<div class="post-metadata">

**Author:** ![x11joe](https://avatars.discourse-cdn.com/v4/letter/x/50afbb/32.png) [@x11joe](https://forum.serverless.com/u/x11joe)\
**Post date:** [November 30, 2017, 6:19pm UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/3 "2017-11-30T18:19:32Z")

</div>

I wasn’t aware you could change the name then change it back, that is an interesting tactic also. This way I don’t have to delete all the others tables, just the one I messed up on. You could add this as an answer on stackoverflow also if you wanted.

---

<div class="post-metadata">

**Author:** ![bfieber](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bfieber/32/653_2.png) [@bfieber](https://forum.serverless.com/u/bfieber)\
**Post date:** [November 30, 2017, 6:44pm UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/4 "2017-11-30T18:44:34Z")

</div>

Be aware that changing the name of the table creates a new table with the new name and drops the original table(unless it has DeletionPolicy: Retain). So be sure to backup your data first if you wish to keep it.

My process for these situations has been:

1. copy the original table and data to mysite-api-phonenumber-dev-bak
2. rename the table in serverless.yml to mysite-api-phonenumber-devX  
2a) sls deploy
3. rename the table in serverless.yml to mysite-api-phonenumber-dev \<== remove the X  
2a) sls deploy
4. restore the data
5. drop the backup table mysite-api-phonenumber-dev-bak

---

<div class="post-metadata">

**Author:** ![cdelgadob](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/cdelgadob/32/291_2.png) [@cdelgadob](https://forum.serverless.com/u/cdelgadob)\
**Post date:** [May 21, 2018, 4:55pm UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/5 "2018-05-21T16:55:11Z")

</div>

Did you use the backup tool in the DynamoDB dashboard? If not, how did you do it?

---

<div class="post-metadata">

**Author:** ![bfieber](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bfieber/32/653_2.png) [@bfieber](https://forum.serverless.com/u/bfieber)\
**Post date:** [May 22, 2018, 8:22pm UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/6 "2018-05-22T20:22:54Z")

</div>

I have a fairly simple node.js script to copy table to table.:

```auto
const AWS = require('aws-sdk')

let dynamodb = new AWS.DynamoDB({ region: 'us-west-2' })

let stage = 'dev' // dev|prod|whatever

var srcParams = {
  TableName: `${stage}-table-to-move`
}
var dstParams = {
  TableName: `${srcParams.TableName}-bak`
}
dynamodb.scan(srcParams, onScan)

function onScan (err, data) {
  if (err) {
    console.error(`Unable to scan ${srcParams.TableName}. Error JSON: `, JSON.stringify(err, null, 2))
  } else {
    console.log('Scan succeeded')
    data.Items.forEach(function (item) {
      dstParams.Item = item
      dynamodb.putItem(dstParams, function (err, data) {
        if (err) console.error(`Unable to add item to ${dstParams.TableName}. Error JSON: `, JSON.stringify(err))
        else console.log('Item ', item, ' written.')
      })
    })

    // continue scanning if we have more items, because
    // scan retrieves a maximum of 1MB of data
    if (typeof data.LastEvaluatedKey !== 'undefined') {
      console.log('Scanning for more...')
      srcParams.ExclusiveStartKey = data.LastEvaluatedKey
      dynamodb.scan(srcParams, onScan)
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![cdelgadob](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/cdelgadob/32/291_2.png) [@cdelgadob](https://forum.serverless.com/u/cdelgadob)\
**Post date:** [May 28, 2018, 9:42am UTC](https://forum.serverless.com/t/cloudformation-cannot-update-a-stack-when-a-custom-named-resource-requires-replacing/3217/7 "2018-05-28T09:42:20Z")

</div>

That looks useful! Thanks!
