DEV Community

Cover image for Script to init aws cdk in a non empty directory
Saad Mahmud
Saad Mahmud

Posted on

Script to init aws cdk in a non empty directory

Recently I tried to run the cdk init command using the aws cdk command line interface, and I came to realize that aws cdk does not allow one to initialize a cdk app in a non-empty directory. Upon conducting a quick google search, I came across this stackoverflow post where I learned that the solution is to initialize in an empty directory and manually merge that directory with the project directory.

Hence, I decided to run this simple powershell script that tries to automate this process. I hope you find this useful and please do let me know your thoughts on this :-). The code for the powershell script is shown below:

$project_location = Get-Location
$temp_app_dir_name = 'infra'
$temp_app_location = Join-Path -Path $project_location -ChildPath $temp_app_dir_name
# Write-Output -InputObject $project_location\requirements.txt

mkdir $temp_app_location

Set-Location -Path $temp_app_location
cdk init app --language python
Set-Location -Path $project_location

# Rename folder so its easier to move out content later
$temp_app_dir_renamed = 'infra_temp'
$temp_app_location_renamed = Join-Path -Path $project_location -ChildPath $temp_app_dir_renamed
Rename-Item -Path $temp_app_location -NewName $temp_app_location_renamed

Get-Content -Path $temp_app_location_renamed\requirements.txt >> $project_location\requirements.txt
Get-Content -Path $temp_app_location_renamed\.gitignore >> $project_location\.gitignore

Remove-Item -Path $temp_app_location_renamed\requirements.txt
Remove-Item -Path $temp_app_location_renamed\.gitignore
Remove-Item -Path $temp_app_location_renamed\README.md
Remove-Item -Path $temp_app_location_renamed\.venv -Recurse

Move-Item -Path $temp_app_location_renamed\* -Destination $project_location

Remove-Item -Path $temp_app_location_renamed

cdk synth
Enter fullscreen mode Exit fullscreen mode

Top comments (0)