Study Guide: Organizing Files and Directory Structures for AWS Deployment
Organize files and a directory structure for application deployment
Organizing Files and Directory Structures for AWS Deployment
Proper organization of application artifacts is a foundational requirement for successful CI/CD and automated deployments in AWS. This guide covers how to structure source code, configuration files, and dependencies to ensure compatibility with services like AWS CodeDeploy, Elastic Beanstalk, and AWS SAM.
Learning Objectives
- Design a standard root directory structure for multi-tier applications.
- Identify mandatory configuration files and their specific locations (e.g.,
appspec.yml,.ebextensions). - Manage dependencies and environment-specific configurations within the package.
- Package Lambda functions and containerized applications for deployment.
Key Terms & Glossary
- Artifact: A compiled or packaged version of the source code (e.g., .zip, .war, .jar, container image) ready for deployment.
- AppSpec File: A configuration file used by AWS CodeDeploy to manage the lifecycle of a deployment.
- ebextensions: A folder containing configuration files (
.config) used to customize AWS Elastic Beanstalk environments. - Lambda Layer: A distribution mechanism for libraries and other dependencies that can be shared across multiple Lambda functions.
- Environment Variables: Key-value pairs used to pass configuration data to applications without changing the code.
The "Big Idea"
In AWS, structure is contract. Services like CodeDeploy or Elastic Beanstalk expect specific files in specific locations to perform automation. If the directory structure is incorrect, the automation "contract" is broken, leading to deployment failures. Organizing your project isn't just about tidiness; it is about providing the metadata the AWS cloud needs to provision and configure resources correctly.
Formula / Concept Box
| Service | Required File/Folder | Default Location | Key Purpose |
|---|---|---|---|
| AWS CodeDeploy | appspec.yml | Root Directory | Defines hooks and file mapping. |
| Elastic Beanstalk | .ebextensions/ | Root Directory | Advanced environment customization. |
| AWS SAM | template.yaml | Root Directory | Infrastructure as Code (IaC) definition. |
| Lambda | index.js / lambda_function.py | Root or defined path | Entry point for execution. |
| Docker/ECS | Dockerfile | Root Directory | Instructions for building the image. |
Hierarchical Outline
- Application Root Structure
- Source Code: Core logic (e.g.,
src/orapp/). - Tests: Unit and integration tests (e.g.,
tests/). - Configuration: Environment-specific settings.
- Source Code: Core logic (e.g.,
- Service-Specific Artifacts
- CodeDeploy (EC2/On-Premise): Requires
appspec.ymland ascripts/folder for lifecycle hooks. - Elastic Beanstalk: Uses
.ebextensions/*.configfor resource tweaks. - Serverless (SAM): Requires
template.yamland often alayer/directory for shared code.
- CodeDeploy (EC2/On-Premise): Requires
- Dependency Management
- Language-Specific:
package.json(Node),requirements.txt(Python),pom.xml(Java). - Vendoring: Packaging dependencies directly into the zip file for Lambda if not using Layers.
- Language-Specific:
Visual Anchors
Deployment Artifact Flow
Folder Structure for CodeDeploy
Definition-Example Pairs
- Lifecycle Hook: A specific point in a deployment where you can run custom scripts.
- Example: Using the
BeforeInstallhook in CodeDeploy to delete old files on an EC2 instance before copying the new version.
- Example: Using the
- Staging Variables: Values used to provide different configuration settings to different stages of a deployment.
- Example: Passing a different Database URL to the
devstage versus theprodstage in Amazon API Gateway.
- Example: Passing a different Database URL to the
- Artifact Packaging: The act of bundling all necessary files (code + libs) into a single deliverable.
- Example: Creating a
.zipfile for a Python Lambda function that includes therequestslibrary inside the root folder.
- Example: Creating a
Worked Examples
Example 1: Structuring a Multi-Tier Application
You are deploying a web application with a frontend (React) and a backend (Node.js). To avoid confusion, separate them into distinct directories within your repository.
Proposed Structure:
/my-application
├── /frontend
│ ├── package.json
│ └── /src
├── /backend
│ ├── appspec.yml
│ ├── package.json
│ └── /src
└── buildspec.yml (for CodeBuild)[!NOTE] Keeping these separate allows for independent CI/CD pipelines for the frontend and backend.
Example 2: Configuring Elastic Beanstalk Extensions
To install a specific Linux package (like git) on your Beanstalk instances, you must use a configuration file in the .ebextensions folder.
File: .ebextensions/01_packages.config
packages:
yum:
git: []This file must be located in the .ebextensions/ folder at the root of the source bundle.
Checkpoint Questions
- Where must the
appspec.ymlfile be located in a CodeDeploy source bundle? - What is the purpose of the
.ebextensionsfolder in an Elastic Beanstalk application? - True or False: A Lambda deployment package must always include its dependencies unless using Lambda Layers.
- Which AWS service uses a
template.yamlfile to define serverless resources? - How do you manage sensitive environment variables (like API keys) across different deployment environments?
▶Click to see answers
- The root directory.
- To customize the EC2 environment and provision additional AWS resources.
- True.
- AWS SAM (Serverless Application Model).
- Use AWS AppConfig, Secrets Manager, or Parameter Store (Systems Manager).