<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohamed Moanis</title>
    <description>The latest articles on DEV Community by Mohamed Moanis (@mmoanis).</description>
    <link>https://dev.to/mmoanis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F793536%2F710f4cd1-c905-4eb8-88c4-d0be5dac9e83.jpeg</url>
      <title>DEV Community: Mohamed Moanis</title>
      <link>https://dev.to/mmoanis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmoanis"/>
    <language>en</language>
    <item>
      <title>How to connect Github to AWS CodePipelines?</title>
      <dc:creator>Mohamed Moanis</dc:creator>
      <pubDate>Sun, 16 Jan 2022 00:19:25 +0000</pubDate>
      <link>https://dev.to/mmoanis/how-to-connect-github-to-aws-codepipelines-2l0h</link>
      <guid>https://dev.to/mmoanis/how-to-connect-github-to-aws-codepipelines-2l0h</guid>
      <description>&lt;p&gt;AWS documentation describes two ways to connect your Github repository to CodePipelines. The &lt;a href="https://docs.aws.amazon.com/codepipeline/latest/userguide/connections-github.html" rel="noopener noreferrer"&gt;first&lt;/a&gt; –recommended– way is easy to follow but it is only available via the AWS console or CLI. If you are using CDK you are left with the second option which I describe here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5a8mxadiiaokxml28cvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5a8mxadiiaokxml28cvx.png" alt="Github loves CodePipelines"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create OAuth token to access your Github repository
&lt;/h2&gt;

&lt;p&gt;The first step is to generate an access token to allow your pipeline to access and read your repository on Github. Follow the Github documentation to create your personal access token as described &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Make sure to select the &lt;code&gt;admin:repo_hook&lt;/code&gt; and &lt;code&gt;repo&lt;/code&gt; permissions to allow your pipeline a read access and also allow it to install a webhook to trigger pipeline actions when you make a new commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Store the OAuth token in AWS Secrets Manager
&lt;/h2&gt;

&lt;p&gt;In the second step, we need to store the token so that we can use it. The access token is an important security credential and therefore you do not want to store it as plain text. Alternatively, we will store it in Secrets Manager. This way you can reference it dynamically in your CDK code.&lt;/p&gt;

&lt;p&gt;Set the access token and the AWS region you are using and create the secret via CLI:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
➜  ~ GITHUB_ACCESS_TOKEN='this is my secret'
➜  ~ REGION=us-east-1
➜  ~ aws secretsmanager  create-secret --name github-access-token-secret --description "Github access token" --secret-string $GITHUB_ACCESS_TOKEN --region $REGION


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Setup a CloudFormation stack to deploy the pipeline
&lt;/h2&gt;

&lt;p&gt;It is important to note that your CodePipeline is by itself a CFN resource that you can define using CDK. In the third step, we define a CFN stack to deploy our pipeline. In this example, I am using NodeJS CDK:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 typescript
import * as cdk from 'aws-cdk-lib';
import { CodeBuildStep, CodePipeline, CodePipelineSource } from 'aws-cdk-lib/pipelines';
import { Construct } from 'constructs';

export class PipelineStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        // Set your Github username and repository name
        const branch = 'master';
        const gitHubUsernameRepository = 'mmoanis/feed-mohamed-app';

        const pipeline = new CodePipeline(this, 'Pipeline', {
            pipelineName: "MyCDKPipeline",
            synth: new CodeBuildStep('SynthStep', {
                input: CodePipelineSource.gitHub(gitHubUsernameRepository, branch, {
                    authentication: cdk.SecretValue.secretsManager('github-access-token-secret'),
                }),
                installCommands: [
                    'npm install -g aws-cdk'
                ],
                commands: [
                    'npm ci',
                    'npm run build',
                    'npx cdk synth'
                ]
            })
        });
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The CFN template will be generated with a dynamic reference to the secret stored in Secrets Manager with your Github personal access token.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Commit your code and deploy your stack
&lt;/h2&gt;

&lt;p&gt;The final step, commit your code and push it to your branch. Then deploy your CFN stack from CLI to bootstrap your pipeline:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
cdk deploy


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once you deploy your CFN stack, your pipeline will start building and deploying your stack for you. Each commit to the specified branch will trigger an action on your pipeline which in turn will mutate it self –if you have changed it.&lt;/p&gt;

&lt;p&gt;I created a complete Github repository with a full example that you can find &lt;a href="https://github.com/mmoanis/feed-mohamed-app" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>node</category>
      <category>github</category>
    </item>
  </channel>
</rss>
