Skip to content

Commit 3ffc34b

Browse files
committedAug 16, 2022
feat: Use AWS with Gitpod 🚀
1 parent 2219df3 commit 3ffc34b

File tree

5 files changed

+144
-196
lines changed

5 files changed

+144
-196
lines changed
 

‎.gitpod.yml

+22-121
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,33 @@
1-
## Learn more about this file at 'https://www.gitpod.io/docs/references/gitpod-yml'
2-
##
3-
## This '.gitpod.yml' file when placed at the root of a project instructs
4-
## Gitpod how to prepare & build the project, start development environments
5-
## and configure continuous prebuilds. Prebuilds when enabled builds a project
6-
## like a CI server so you can start coding right away - no more waiting for
7-
## dependencies to download and builds to finish when reviewing pull-requests
8-
## or hacking on something new.
9-
##
10-
## With Gitpod you can develop software from any device (even iPads) via
11-
## desktop or browser based versions of VS Code or any JetBrains IDE and
12-
## customise it to your individual needs - from themes to extensions, you
13-
## have full control.
14-
##
15-
## The easiest way to try out Gitpod is install the browser extenion:
16-
## 'https://www.gitpod.io/docs/browser-extension' or by prefixing
17-
## 'https://gitpod.io#' to the source control URL of any project.
18-
##
19-
## For example: 'https://gitpod.io#https://github.com/gitpod-io/gitpod'
20-
21-
## The 'image' section defines which Docker image Gitpod should use.
22-
## By default, Gitpod uses a standard Docker Image called 'workspace-full'
23-
## which can be found at 'https://github.com/gitpod-io/workspace-images'
24-
##
25-
## Workspaces started based on this default image come pre-installed with
26-
## Docker, Go, Java, Node.js, C/C++, Python, Ruby, Rust, PHP as well as
27-
## tools such as Homebrew, Tailscale, Nginx and several more.
28-
##
29-
## If this image does not include the tools needed for your project then
30-
## a public Docker image or your own Docker file can be configured.
31-
##
32-
## Learn more about images at 'https://www.gitpod.io/docs/config-docker'
33-
34-
#image: node:buster # use 'https://hub.docker.com/_/node'
35-
#
36-
#image: # leave image undefined if using a Dockerfile
37-
# file: .gitpod.Dockerfile # relative path to the Dockerfile from the
38-
# # root of the project
39-
40-
image:
41-
file: .gitpod/.gitpod.Dockerfile
42-
43-
## The 'tasks' section defines how Gitpod prepares and builds this project
44-
## or how Gitpod can start development servers. With Gitpod, there are three
45-
## types of tasks:
46-
##
47-
## - before: Use this for tasks that need to run before init and before command.
48-
## - init: Use this to configure prebuilds of heavy-lifting tasks such as
49-
## downloading dependencies or compiling source code.
50-
## - command: Use this to start your database or application when the workspace starts.
51-
##
52-
## Learn more about these tasks at 'https://www.gitpod.io/docs/config-start-tasks'
53-
54-
#tasks:
55-
# - before: |
56-
# # commands to execute...
57-
#
58-
# - init: |
59-
# # sudo apt-get install python3 # can be used to install operating system
60-
# # dependencies but these are not kept after the
61-
# # prebuild completes thus Gitpod recommends moving
62-
# # operating system dependency installation steps
63-
# # to a custom Dockerfile to make prebuilds faster
64-
# # and to keep your codebase DRY.
65-
# # 'https://www.gitpod.io/docs/config-docker'
1+
# Learn more about this file at 'https://www.gitpod.io/docs/references/gitpod-yml'
662
#
67-
# # pip install -r requirements.txt # install codebase dependencies
68-
# # cmake # precompile codebase
3+
# This '.gitpod.yml' file when placed at the root of a project instructs
4+
# Gitpod how to prepare & build the project, start development environments
5+
# and configure continuous prebuilds. Prebuilds when enabled builds a project
6+
# like a CI server so you can start coding right away - no more waiting for
7+
# dependencies to download and builds to finish when reviewing pull-requests
8+
# or hacking on something new.
699
#
70-
# - name: Web Server
71-
# openMode: split-left
72-
# env:
73-
# WEBSERVER_PORT: 8080
74-
# command: |
75-
# python3 -m http.server $WEBSERVER_PORT
76-
#
77-
# - name: Web Browser
78-
# openMode: split-right
79-
# env:
80-
# WEBSERVER_PORT: 8080
81-
# command: |
82-
# gp await-port $WEBSERVER_PORT
83-
# lynx `gp url`
10+
# With Gitpod you can develop software from any device (even iPads) via
11+
# desktop or browser based versions of VS Code or any JetBrains IDE and
12+
# customise it to your individual needs - from themes to extensions, you
13+
# have full control.
8414

8515
tasks:
16+
- name: Initialize & Configure AWS
17+
init: bash $GITPOD_REPO_ROOT/configure_aws_with_gitpod.sh
8618
- name: AWS SSO Login
8719
openMode: split-left
88-
command: |
89-
aws sso login
90-
- name: Bash
20+
command: aws sso login
21+
- name: Bash Terminal
9122
openMode: split-right
92-
command: |
93-
bash
94-
95-
## The 'ports' section defines various ports your may listen on are
96-
## configured in Gitpod on an authenticated URL. By default, all ports
97-
## are in private visibility state.
98-
##
99-
## Learn more about ports at 'https://www.gitpod.io/docs/config-ports'
100-
101-
#ports:
102-
# - port: 8080 # alternatively configure entire ranges via '8080-8090'
103-
# visibility: private # either 'public' or 'private' (default)
104-
# onOpen: open-browser # either 'open-browser', 'open-preview' or 'ignore'
23+
command: bash
10524

106-
## The 'vscode' section defines a list of Visual Studio Code extensions from
107-
## the OpenVSX.org registry to be installed upon workspace startup. OpenVSX
108-
## is an open alternative to the proprietary Visual Studio Code Marketplace
109-
## and extensions can be added by sending a pull-request with the extension
110-
## identifier to https://github.com/open-vsx/publish-extensions
111-
##
112-
## The identifier of an extension is always ${publisher}.${name}.
113-
##
114-
## For example: 'vscodevim.vim'
115-
##
116-
## Learn more at 'https://www.gitpod.io/docs/ides-and-editors/vscode'
117-
118-
#vscode:
119-
# extensions:
120-
# - vscodevim.vim
121-
# - esbenp.prettier-vscode@9.5.0
122-
# - https://example.com/abc/releases/extension-0.26.0.vsix
123-
124-
## The 'github' section defines configuration of continuous prebuilds
125-
## for GitHub repositories when the GitHub application
126-
## 'https://github.com/apps/gitpod-io' is installed in GitHub and granted
127-
## permissions to access the repository.
128-
##
129-
## Learn more at 'https://www.gitpod.io/docs/prebuilds'
25+
# The 'github' section defines configuration of continuous prebuilds
26+
# for GitHub repositories when the GitHub application
27+
# 'https://github.com/apps/gitpod-io' is installed in GitHub and granted
28+
# permissions to access the repository.
29+
#
30+
# Learn more at 'https://www.gitpod.io/docs/prebuilds'
13031

13132
github:
13233
prebuilds:

‎.gitpod/.gitpod.Dockerfile

-27
This file was deleted.

‎.gitpod/.gitpod.configure.sh

-37
This file was deleted.

‎README.md

+21-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
Ever wondered how to integrate AWS Single Sign-On (SSO) and [Amazon Elastic Container Registry (ECR)](https://aws.amazon.com/ecr/) with Gitpod? Here's how...
44

5-
## Demo
6-
7-
<a href="https://gitpod.io/#https://github.com/gitpod-io/demo-gitpod-with-aws"><img src="https://gitpod-staging.com/button/open-in-gitpod.svg"/></a>
8-
9-
```bash
10-
$ command + command output that demonstrates pushing/pulling to/from ECR in Gitpod
11-
```
5+
## Secret Management
126

13-
### Secret Management
7+
### Secrets Key-Value Map
148

159
```bash
1610
sso_start_url = ${AWS_SSO_URL}
@@ -20,9 +14,25 @@ sso_role_name = ${AWS_ROLE_NAME}
2014
region = ${AWS_REGION}
2115
```
2216

23-
1. configure your secrests in gitpod here.
24-
2. Maybe you use vault or some other secret storage, that's okay. the key is to inject them into the config when the worksapce starts. heres how.
17+
### Configure secrets
18+
19+
- Configure the following secrets [here in Gitpod settings](https://gitpod.io/variables):
20+
21+
- `AWS_SSO_URL`
22+
- `AWS_SSO_REGION`
23+
- `AWS_ACCOUNT_ID`
24+
- `AWS_ROLE_NAME`
25+
- `AWS_REGION`
26+
27+
<br>
28+
29+
> **Note**: You Can set scope at for all worskapces (⚠️) or at your Org. Level, at your Personal username level, or at Just Repo. Level. _Read More: [Configure Environment Variables](https://www.gitpod.io/docs/environment-variables#using-the-account-settings)_
30+
31+
- Maybe you use vault or some other secret storage, that's okay. the key is to inject them into the config when the workspace starts.
32+
33+
## Demo
2534

35+
<a href="https://gitpod.io/#https://github.com/gitpod-io/demo-gitpod-with-aws"><img src="https://gitpod.io/button/open-in-gitpod.svg"/></a>
2636

2737
## Recommended Reading
2838

@@ -31,7 +41,7 @@ region = ${AWS_REGION}
3141
- [One workspace per task](https://www.gitpod.io/docs/workspaces)
3242
- [Environment variables](https://www.gitpod.io/docs/environment-variables#using-the-account-settings)
3343
- [Custom Docker Image](https://www.gitpod.io/docs/config-docker)
34-
- [.gitpod.yml](https://www.gitpod.io/docs/config-gitpod-file)
44+
- [Config `.gitpod.yml`](https://www.gitpod.io/docs/config-gitpod-file)
3545

3646
### AWS
3747

‎configure_aws_with_gitpod.sh

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# update AWS CLI
5+
OLD_DIR="$PWD"
6+
TMP_DIR="$(mktemp -d)"
7+
echo "Updating AWS"
8+
cd "${TMP_DIR}" || exit 1
9+
10+
curl -fSsl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
11+
unzip -qq awscliv2.zip
12+
sudo ./aws/install --update
13+
rm awscliv2.zip
14+
15+
cd "${OLD_DIR}" || exit 1
16+
rm -rf "${TMP_DIR}"
17+
18+
# make sure we have ecr-login
19+
if [ ! -f /usr/local/bin/docker-credential-ecr-login ]; then
20+
echo "Installing ecr-login helper"
21+
OLD_DIR="$PWD"
22+
TMP_DIR="$(mktemp -d)"
23+
cd "${TMP_DIR}" || exit 1
24+
ECR_LATEST=$(curl -s https://api.github.com/repos/awslabs/amazon-ecr-credential-helper/releases/latest | jq -r ".tag_name")
25+
curl -o docker-credential-ecr-login -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-amd64/docker-credential-ecr-login"
26+
curl -o docker-credential-ecr-login.sha256 -fSsL "https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/${ECR_LATEST##*v}/linux-amd64/docker-credential-ecr-login.sha256"
27+
sha256sum -c docker-credential-ecr-login.sha256
28+
sudo mv docker-credential-ecr-login /usr/local/bin/docker-credential-ecr-login
29+
sudo chmod +x /usr/local/bin/docker-credential-ecr-login
30+
cd "${OLD_DIR}" || exit 1
31+
rm -rf "${TMP_DIR}"
32+
fi
33+
34+
# This should be moved to the workspace image.
35+
if ! command -v session-manager-plugin; then
36+
echo "Installing AWS session manager plugin"
37+
38+
TMP_DIR="$(mktemp -d)"
39+
cd "$TMP_DIR" || exit 1
40+
41+
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
42+
sudo dpkg -i "session-manager-plugin.deb"
43+
44+
cd "$OLD_DIR"
45+
rm -rf "$TMP_DIR"
46+
fi
47+
48+
49+
AWS_VARS=(AWS_SSO_URL AWS_SSO_REGION AWS_ACCOUNT_ID AWS_ROLE_NAME AWS_REGION)
50+
51+
for AWS_VAR in "${AWS_VARS[@]}"; do
52+
echo "$AWS_VAR is ${!AWS_VAR}"
53+
if [[ -z "${!AWS_VAR}" ]]; then
54+
echo "Error: AWS variable \"$AWS_VAR\" is unset"
55+
AWS_VAR_UNSET=true
56+
fi
57+
done
58+
59+
if ! [[ -z "$AWS_VAR_UNSET" ]]; then
60+
SCRIPT=$(realpath "$0")
61+
echo "AWS Variables are not set, skipping autoconfig of files."
62+
echo "Re-run ${SCRIPT} when AWS_ variables are set."
63+
echo "set you AWS_ variables in https://gitpod.io/variables ."
64+
echo "For more help, you can refer these docs: https://www.gitpod.io/docs/environment-variables#using-the-account-settings ."
65+
exit 1
66+
fi
67+
68+
69+
# create the config for SSO login
70+
71+
# This assumes the below variables have been configured for this repo in gitpod
72+
# https://www.gitpod.io/docs/environment-variables#using-the-account-settings
73+
echo "Forcing AWS config to just use SSO credentials"
74+
[[ -d /home/gitpod/.aws ]] || mkdir /home/gitpod/.aws
75+
cat <<- AWSFILE > /home/gitpod/.aws/config
76+
[default]
77+
sso_start_url = ${AWS_SSO_URL}
78+
sso_region = ${AWS_SSO_REGION}
79+
sso_account_id = ${AWS_ACCOUNT_ID}
80+
sso_role_name = ${AWS_ROLE_NAME}
81+
region = ${AWS_REGION}
82+
AWSFILE
83+
84+
# update docker config to use ecr-login
85+
# if we don't have a .docker/config.json, create:
86+
87+
if [ ! -d /home/gitpod/.docker ]; then
88+
mkdir -p /home/gitpod/.docker && echo '{}' > /home/gitpod/.docker/config.json
89+
elif [ ! -f /home/gitpod/.docker/config.json ]; then
90+
echo '{}' > /home/gitpod/.docker/config.json
91+
fi
92+
93+
echo "Ensuring Docker Config uses ecr-login for ECR repositories"
94+
95+
cp /home/gitpod/.docker/config.json /home/gitpod/.docker/config_bak.json
96+
jq '.credHelpers["public.ecr.aws"]="ecr-login"' /home/gitpod/.docker/config.json > /home/gitpod/.docker/config_tmp.json
97+
jq ".credHelpers[\"${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com\"]=\"ecr-login\"" /home/gitpod/.docker/config_tmp.json > /home/gitpod/.docker/config.json
98+
rm /home/gitpod/.docker/config_tmp.json
99+
100+
echo "All Things whcih are required for AWS SSO & ECR Login are Installed & Configured Successfully."
101+
echo "Now, You can Start an AWS SSO login session."

0 commit comments

Comments
 (0)
Please sign in to comment.