Skip to content

Commit

Permalink
Adding an extra learning challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 16, 2022
1 parent ced9e90 commit 0e093af
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,25 @@ And you can get logs for the service using `copilot svc logs`

Bonus Points: Try deploying another copy of the load test job, but this time make the load test target the AWS App Runner deployment.

## Step Eight: Tear everything down
## Extra Learning

If you are down for an extra challenge, then try deploying the application in the `hit-counter` folder.
This containerized application implements a simple hit counter that counts the number of requests the
application receives. It stores its state inside of a DynamoDB table.

Hint: You can use the `copilot storage init` command to automatically create a DynamoDB table for the application
prior to deploying it using AWS Copilot. The application is expecting:

```
Table Name: hits
Table primary key: counter
Table primary key type: string
```

Copilot will automatically take care of creating the permissions between the application and the table. See if you
can deploy this more complicated application!

## Final Step: Tear everything down

If you want to clean everything up you can go back to Cloud9 and run:

Expand Down
1 change: 1 addition & 0 deletions hit-counter/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions hit-counter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM public.ecr.aws/docker/library/node:18 AS build
WORKDIR /srv
ADD package.json .
RUN npm install

FROM public.ecr.aws/docker/library/node:18-slim
COPY --from=build /srv .
ADD . .
EXPOSE 80
CMD ["node", "index.js"]
49 changes: 49 additions & 0 deletions hit-counter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var aws = require('aws-sdk');
const express = require('express');
const app = express();
const table = process.env.HITS_NAME;
const PORT = 80; // The port to listen on

const dynamodb = new aws.DynamoDB.DocumentClient();

if (!table) {
console.error('Warning: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table');
}

const os = require('os');
const hostname = os.hostname();

app.get('*', function (req, res) {
if (!table) {
console.error('Error: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table');
return res.send('Error: The HITS_NAME environment variable needs to be set with the name of a DynamoDB table');
}

dynamodb.update({
TableName: table,
Key: {
counter: 'global',
},
UpdateExpression: 'SET hitCount = if_not_exists(hitCount, :zero) + :value',
ExpressionAttributeValues: {
':zero': 0,
':value': 1,
},
ReturnValues: 'ALL_NEW'
}, function (err, results) {
if (err) {
return res.send(err);
} else {
var hitCount = results.Attributes.hitCount;
res.send(`There have been ${hitCount} hits. (${hostname})`);
}
});
});

app.listen(PORT, () => console.log(`Listening on port ${PORT}!`));

// This causes the process to respond to "docker stop" faster
process.on('SIGTERM', function () {
console.log('Received SIGTERM, shutting down');
app.close();
});
Loading

0 comments on commit 0e093af

Please sign in to comment.