Idiopathic pulmonary fibrosis (IPF) is a progressive fibrotic disease of unknown pathogenic origin. Endoplasmic reticulum (ER) stress refers to the process by which cells take measures to ER function when the morphology and function of the reticulum are changed. Recent studies have demonstrated that the ER was involved in the evolution and progression of IPF. In this study, we obtained transcriptome data and relevant clinical information from the Gene Expression Omnibus database and conducted bioinformatics analysis. Among the 544 ER stress-related genes (ERSRGs), 78 were identified as differentially expressed genes (DEGs). These DEGs were primarily enriched in response to ER stress, protein binding, and protein processing. Two genes (HTRA2 and KTN1) were included for constructing an accurate molecular signature. The overall survival of patients was remarkably worse in the high-risk group than in the low-risk group. We further analyzed the difference in immune cells between high-risk and low-risk groups. M0 and M2 macrophages were significantly increased in the high-risk group. Our results suggested that ERSRGs might play a critical role in the development of IPF by regulating the immune microenvironment in the lungs, which provide new insights on predicting the prognosis of patients with IPF.