# Upgrade Code Ocean

## Prerequisites

To upgrade to a newer version of Code Ocean, please contact support at <support@codeocean.com> and request to have the Code Ocean’s AMIs shared with your AWS account.

## Stop Running Computations

Before you start to update your Code Ocean application stack, you need to make sure there are no computations running, to ensure a smooth upgrade.

Using AWS Session Manager, connect to Code Ocean **services** instance :

![](https://1555061772-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQOseC04KrGrSsjahquVQ%2Fuploads%2Fgit-blob-a4c6bfcf1c2e9704040f927bf5d041aebc67473b%2FScreenshot%202021-08-17%20at%2012.28.26.png?alt=media)

Run the following command to stop computations :

```
curl -v -X POST localhost:8300/cancel_computations
```

Wait until all computations complete by waiting until the following CloudWatch metric reaches 0: `CloudWatch > Metrics > CodeOcean > AutoScalingGroupName > SlotsUtilization`

## Remove scale-in Protection from Code Ocean Worker EC2 Instances

The upgrade waits for existing running workers to terminate before continuing. If you verified that there are no running computations in the previous step, you can safely remove the scale in protection from the workers in the auto scaling group to complete the upgrade quicker.

In the AWS EC2 console go to `EC2 > Auto Scaling Groups > workers-[id]-asg > Instance Management`, then select all instances and click `Actions > Remove scale-in protection`

Then in `EC2 > Auto Scaling Groups > workers-[id]-asg > Details` reduce the Desired capacity to 0.

## Verify there are no hanging jobs in AWS Batch

There are five AWS Batch Compute Environments in Code Ocean VPC deployment, each with a corresponding job queue. Before starting the CFN update, you should verify that there are no jobs in `Runnable` or `Running` status in any of those queues:

1. BatchOnDemandJobQueue
2. BatchGpuOnDemandJobQueue
3. BatchSpotJobQueue
4. BatchGpuSpotJobQueue
5. BatchJobsJobQueue

`AWS Batch > Dashboard > Job queue overview`, then for each of the  5 Code Ocean deployment's queues mentioned above, press on the number of jobs under `Running` or `Runnable`, then cancel any job in `Runnable` or `Running` status:

<figure><img src="https://1555061772-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQOseC04KrGrSsjahquVQ%2Fuploads%2FnlrhIxipDmVSvDGyWncb%2Fimage.png?alt=media&#x26;token=6b669c2b-5255-48d7-a115-304137dca0f0" alt=""><figcaption></figcaption></figure>

## Update the CloudFormation Stack

{% hint style="info" %}
Note : If you are using a least privileged IAM role to update your CloudFormation stack, make sure to first update your least privileged IAM role from [here](https://docs.codeocean.com/admin-guide/v2.18/deployment-guide/deployment-iam-role)
{% endhint %}

Upgrading Code Ocean is done by updating the CloudFormation stack you've [created upon deployment](https://docs.codeocean.com/admin-guide/v2.18/cloudformation-deployment#create-cloudformation-stack) :

![](https://1555061772-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQOseC04KrGrSsjahquVQ%2Fuploads%2Fgit-blob-ed12d965eaeeb393726d23d33eef4e93f77599d8%2FScreenshot%202021-08-17%20at%2010.49.52.png?alt=media)

On the next screen, select **Replace current template**, then **Amazon S3 URL,** and specify the CloudFormation template URL. You can find the URL in [Step 1 - Specify the template](https://docs.codeocean.com/admin-guide/v2.18/cloudformation-deployment#step-1-specify-the-template) section, or use a URL you've received from our support team:

![](https://1555061772-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQOseC04KrGrSsjahquVQ%2Fuploads%2Fgit-blob-eb4a43e05dc4d6d1648a82795aa689376a6450b8%2Fimage%20\(72\).png?alt=media)

Click **Next** when you are done.

### Specify Stack Details

{% hint style="warning" %}
**Stack's Parameters** will be set to the values that have been set upon the latest upgrade/deployment, or to their recommended defaults. Please visit the [Deployment Parameters](https://docs.codeocean.com/admin-guide/v2.18/deployment-guide/deployment-parameters) page for the full list of parameters and their purpose. Please contact our support or email <support@codeocean.com> if you have any questions or you need help regarding parameters values.
{% endhint %}

Click **Next**.

### Configure Stack Options (Optional)

You can specify tags (key-value pairs) for resources in your stack and set advanced options.

Click **Next** when you are done.

### Review

1. Review and assure that all the details are correct.
2. Check the box to acknowledge that CloudFormation might create IAM resources.
3. Click **Update stack** to start the deployment.

After you click on Update stack, you will be redirected to the stack screen where you can follow the progress of your stack update under the **Events** tab.

### Upgrade Completion

When the stack update is finished, the state of the CloudFormation stack will turn into `UPDATE_COMPLETE` .
