Skip to content

Commit affaddc

Browse files
document github actions deployment to aws
1 parent 962b3e1 commit affaddc

File tree

4 files changed

+193
-84
lines changed

4 files changed

+193
-84
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Deploying from GitHub Actions
3+
description: Using the Defang Github Action to deploy your project from your CI/CD pipeline.
4+
---
5+
6+
# Deploying from GitHub Actions
7+
8+
Defang makes it easy to deploy your applications directly from your GitHub Actions workflow using the [Defang GitHub Action](https://github.com/DefangLabs/defang-github-action).
9+
10+
There is a dedicated tutorial for deploying to each cloud provider:
11+
* [AWS](/docs/tutorials/deploying-from-github-actions/to-aws.md)
12+
* [GCP](/docs/tutorials/deploying-from-github-actions/to-gcp.md).
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: Deploying to AWS from GitHub Actions
3+
description: Using the Defang Github Action to deploy your project to AWS from your CI/CD pipeline.
4+
---
5+
6+
# Deploying to AWS from GitHub Actions
7+
8+
This tutorial will show you how to use the [Defang GitHub Action](https://github.com/DefangLabs/defang-github-action) to deploy your project to AWS from your GitHub Actions workflow.
9+
10+
## Prerequisites
11+
12+
- [A Defang Account](/docs/concepts/authentication)
13+
- [A Github Repo](https://docs.github.com/en/get-started/quickstart/create-a-repo)
14+
- [An AWS Account](https://aws.amazon.com)
15+
16+
The following steps will guide you through setting up a GitHub Actions workflow that can assume a role in your AWS account using OpenID Connect (OIDC) and deploy your project using the Defang GitHub Action. The role which will be assumed must have a trust relationship with an OIDC identity provider (IdP) for GitHub Actions, and that trust relationship must be configured to allow the specific repository and branch to assume the role. This ultimately allows the GitHub Actions workflow to securely access your AWS resources without needing to store long-lived AWS credentials in your repository.
17+
18+
## Step 1 - Identify your AWS Account ID
19+
20+
To configure the GitHub Action to assume a role in your AWS account, you'll need your AWS Account ID.
21+
22+
<Tabs>
23+
<TabItem value="cli" label="AWS CLI" default>
24+
```bash
25+
aws sts get-caller-identity --query Account --output text
26+
123456789012 # for example
27+
```
28+
</TabItem>
29+
<TabItem value="dashboard" label="AWS Dashboard">
30+
1. Go to the [AWS Management Console](https://aws.amazon.com/console/).
31+
2. In the top right corner, click on your account name or number.
32+
3. Your AWS Account ID will be displayed in the dropdown menu.
33+
</TabItem>
34+
</Tabs>
35+
36+
## Step 2 - Create an AWS Identity Provider for GitHub Actions
37+
38+
You will need to create a new OIDC Identity Provider in AWS to enable GitHub Actions to assume roles in your AWS account.
39+
40+
<Tabs>
41+
<TabItem value="cli" label="AWS CLI" default>
42+
Using the AWS CLI:
43+
44+
```
45+
aws iam create-open-id-connect-provider --client-id-list sts.amazonaws.com --url https://token.actions.githubusercontent.com
46+
```
47+
</TabItem>
48+
<TabItem value="dashboard" label="AWS Dashboard">
49+
Using the AWS Dashboard:
50+
51+
1. Go to the [AWS IAM Console](https://console.aws.amazon.com/iam/home#/roles).
52+
2. Click on "Identity providers" in the left sidebar.
53+
3. Click on "Add provider".
54+
4. Choose "OIDC" as the provider type.
55+
5. For the provider URL, enter `https://token.actions.githubusercontent.com`.
56+
6. For the audience, enter `sts.amazonaws.com`.
57+
7. Click "Add provider".
58+
</TabItem>
59+
</Tabs>
60+
61+
## Step 3 - Create a deployer role with trust relationship for GitHub Actions
62+
63+
<Tabs>
64+
<TabItem value="cli" label="AWS CLI" default>
65+
Using the AWS CLI:
66+
67+
1. Create a trust policy document
68+
69+
```bash
70+
cat > deployer-policy.json << EOF
71+
{
72+
"Version": "2012-10-17",
73+
"Statement": [
74+
{
75+
"Effect": "Allow",
76+
"Action": "*",
77+
"Resource": "*"
78+
},
79+
{
80+
"Sid": "OidcForGitHub",
81+
"Effect": "Allow",
82+
"Principal": {
83+
"Federated": "arn:aws:iam::YOUR_AWS_ACCOUNT_ID:oidc-provider/token.actions.githubusercontent.com"
84+
},
85+
"Action": "sts:AssumeRoleWithWebIdentity",
86+
"Condition": {
87+
"StringLike": {
88+
"token.actions.githubusercontent.com:sub": "repo:YOUR_REPO_OWNER/YOUR_REPO_NAME:ref:refs/heads/YOUR_BRANCH_NAME"
89+
},
90+
"StringEquals": {
91+
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
92+
}
93+
}
94+
}
95+
]
96+
}
97+
EOF
98+
```
99+
100+
2. Edit the `deployer-policy.json` file to replace the following placeholders:
101+
* `YOUR_AWS_ACCOUNT_ID` replace this with your actual AWS Account ID
102+
* `YOUR_REPO_OWNER` your GitHub username or organization name (e.g., `ACMELabs`)
103+
* `YOUR_REPO_NAME` your GitHub repository name (e.g., `my-project`)
104+
* `YOUR_BRANCH_NAME` the branch you want to deploy from (e.g., `main`). If you want to allow multiple branches, you can use a wildcard like `*`
105+
106+
3. Create a deployer role
107+
```
108+
aws iam create-role --role-name deployer --assume-role-policy-document file://deployer-policy.json
109+
```
110+
111+
</TabItem>
112+
<TabItem value="dashboard" label="AWS Dashboard">
113+
Using the AWS Dashboard:
114+
115+
1. Navigate to [AWS IAM Console](https://console.aws.amazon.com/iam/home#/roles).
116+
2. Click on "Create role".
117+
3. Select "Web identity" as the trusted entity type.
118+
4. For the identity provider, select the OIDC provider you created in the previous step.
119+
5. For the audience, enter `sts.amazonaws.com`.
120+
6. For the GitHub organization, enter your GitHub username or organization name (e.g., `ACMELabs`).
121+
7. For the GitHub repository, enter your GitHub repository name (e.g., `my-project`).
122+
8. For the GitHub branch, enter the branch you want to deploy from (e.g., `main`). If you want to allow multiple branches, you can use a wildcard like `*`.
123+
9. Click "Next".
124+
10. Select the `AdministratorAccess` policy to attach to the role.
125+
11. Click "Next".
126+
12. For the role name, enter `deployer`.
127+
13. For the role description, enter "This role is assumed by GitHub Actions when deploying with Defang".
128+
13. Click "Create role".
129+
130+
</TabItem>
131+
</Tabs>
132+
133+
## Step 4 - Create a new GitHub Actions workflow
134+
135+
In your GitHub repository, create a new file at `.github/workflows/deploy.yml` with the following content:
136+
137+
```yaml
138+
name: Deploy with Defang
139+
on:
140+
push:
141+
branches:
142+
- main # Change this to your default branch if it's not 'main', this must match the branch you specified in the deployer role's trust relationship.
143+
jobs:
144+
deploy:
145+
runs-on: ubuntu-latest
146+
permissions:
147+
contents: read
148+
id-token: write
149+
150+
steps:
151+
- name: Configure AWS Credentials for CI
152+
uses: aws-actions/configure-aws-credentials@v4
153+
with:
154+
aws-region: us-west-2
155+
# Replace with your AWS Account ID and the name of the role which we previously created.
156+
role-to-assume: arn:aws:iam::123456789012:role/deployer
157+
158+
- name: Checkout Repo
159+
uses: actions/checkout@v4
160+
161+
- name: Deploy
162+
uses: DefangLabs/[email protected]
163+
with:
164+
provider: "aws"
165+
```
166+
167+
:::info
168+
Full documentation for configuring AWS can be found in the [Defang GitHub Action repository](https://github.com/DefangLabs/defang-github-action).
169+
:::
170+
171+
Now you have configured a GitHub Actions workflow that uses the Defang GitHub Action to deploy your project to AWS securely using OIDC and short-lived credentials. Whenever you push to the specified branch, the workflow will run and deploy your project using the permissions granted to the `deployer` role in your AWS account.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Deploying to GCP from GitHub Actions
3+
description: Using the Defang Github Action to deploy your project to GCP from your CI/CD pipeline.
4+
---
5+
6+
# Deploying to GCP from GitHub Actions
7+
8+
:::info
9+
Coming soon
10+
:::

docs/tutorials/github-actions.md

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)